遍历work目录及子目录中扩展名为txt的文件,将它们拷贝到一个新的文件夹work_txt中
#!/usr/bin/env python # -*- coding: utf-8 -*- import os import shutil spath = 'f:\\work' dpath = 'f:\\work_txt' if not os.path.exists(dpath): os.mkdir(dpath) for root, dirs, files in os.walk(spath): for f in files: if f.endswith('.txt'): print(os.path.join(root, f)) txt = os.path.join(root, f) shutil.copy(txt, dpath)