第9章 选择性拷贝

'''快速拷贝:
①. 编写一个程序,遍历一个目录树,查找特定扩展名的文件(诸如.txt或.jpg)。
②. 不论这些文件的位置在哪里, 将它们拷贝到一个新的文件夹中。'''
import os,re,shutil
path = 'E:\\04.AutomationProject'
folder = "E:\\test"
if os.path.exists(folder):
    print("目录存在")
else:
    os.mkdir("E:\\test")
    print("目录创建成功!")
for currentFolder,subFolder,filenames in os.walk(path):
    for fileName in filenames:
        mo = re.compile('.*\.txt$').search(fileName)#\.txt表示文本类型个,可以更改成\.pdf or \.jpg
        if mo == None:
            continue
        else:
            print("%s\%s"%(currentFolder,mo.group()))
            fillName = currentFolder + "\\" +mo.group()#拼接文件txt文件的绝对路径
            shutil.copy(fillName,"E:\\test")

猜你喜欢

转载自blog.csdn.net/baidu_27361307/article/details/80974289