вторник, 19 мая 2015 г.

python и рекурсивная работа с файлами

1) os.listdir
import os
for filename in os.listdir(os.getcwd()):
if filename.endswith(".txt"):
print(os.path.join(root, file))

2) os.walk
import os
for root, dirs, files in os.walk(yourpath, topdown=False):
    for name in files:
        print(os.path.join(root, name))
        stuff
    for name in dirs:
        print(os.path.join(root, name))
        stuff

3) glob
import glob
for filename in glob.glob('*.txt'):
...

http://stackoverflow.com/questions/18262293/python-open-every-file-in-a-folder
http://stackoverflow.com/questions/3964681/find-all-files-in-directory-with-extension-txt-with-python

Комментариев нет:

Отправить комментарий