**Python实验:选择性复制文件**
import shutil, os, re
filedir = 'C:\\Users\\Loyu\\Desktop\\python1'
desfiledir = 'C:\\Users\\Loyu\\Desktop\\file_copy'
totle = 0
for n, f, file in os.walk(filedir):
if len(file) != 0:
filestr = ' '.join(file)
filerex = re.findall(r'\S+.pdf|\S+.txt', filestr)
if len(filerex) != 0:
for i in filerex:
filetotledir = n +'\\' + i
print(filetotledir)
shutil.copy(filetotledir, desfiledir)
totle += 1
else:
continue
print("总共找到 %d 个符合条件的文件" % totle)
totle = 0
for n, f, file in os.walk(filedir):
if len(file) != 0:
for i in file:
if '.pdf' in i or '.txt' in i:
filetotledir = n +'\\' + i
print(filetotledir)
shutil.copy(filetotledir, desfiledir)
totle += 1
else:
continue
print("总共找到 %d 个符合条件的文件" % totle)
转载于:https://blog.51cto.com/8789878/1932416