对于多级目录的文件处理,walk是神器,一个模板如下:
# -*- coding: utf-8
import os
if __name__ == '__main__':
for root, dirs, files in os.walk(r"D:\zaq12wsx\work\year_2020", topdown=True):
for name in files:
if r'.txt' in name:
print(os.path.join(root, name))
for name in dirs:
print(os.path.join(root, name))
参考文献