编写一个程序,遍历一个目录树,查找特定扩展名的文件(诸如.pdf 或.jpg)。不论这些文件的位置在哪里,将它们拷贝到一个新的文件夹中。
import shutil, os
newfile = os.path.abspath('.') + '\\' + 'new'
os.mkdir(newfile)
for folderName, subfolders, filenames in os.walk('.\\a'):
print('The current folder is ' + folderName)
for filename in filenames:
if filename.endswith('.pdf') or filename.endswith('.jpg'):
print('FILE FINDED: ' + filename)
file = os.path.abspath('.') + '\\' + folderName + '\\' + filename
shutil.copy(file, newfile)