Estoy luchando con el resultado del método Path.glob() del módulo Pathlib en Python 3.6.
from pathlib import Path dir = Path.cwd() files = dir.glob('*.txt') print(list(files)) >> [WindowsPath('C:/whatever/file1.txt'), WindowsPath('C:/whatever/file2.txt')] for file in files: print(file) print('Check.') >>Evidentemente, glob encontró archivos, pero el ciclo for no se ejecuta. ¿Cómo puedo recorrer los resultados de una búsqueda pathlib-glob?
>>> from pathlib import Path >>> >>> dir = Path.cwd() >>> >>> files = dir.glob('*.txt') >>> >>> type(files) <class 'generator'> Aquí, los files son un generator , que solo se pueden leer una vez y luego se agotan. Entonces, cuando intentes leerlo por segunda vez, no lo tendrás.
>>> for i in files: ... print(i) ... /home/ahsanul/test/hello1.txt /home/ahsanul/test/hello2.txt /home/ahsanul/test/hello3.txt /home/ahsanul/test/b.txt >>> # let's loop though for the 2nd time ... >>> for i in files: ... print(i) ... >>>