桌面的数据整理 基于文件类型进行归类
office3_02.py
from os import walk, mkdir, remove
import os
from shutil import copy, move
# 桌面的数据整理 基于名字
def copyFile(path, newpath):
for filepath, folders, files in walk(path):
for file in files:
if 'xlsx' in file:
if os.path.exists(newpath+'\\xlsx'):
print(os.path.join(filepath), file)
copy(os.path.join(filepath,file), os.path.join(newpath+'\\xlsx',file))
else:
print('xlsx folder is not exitsed and create folder')
os.mkdir(newpath+'\\xlsx')
copy(os.path.join(filepath, file), os.path.join(newpath + '\\xlsx', file))
if 'pdf' in file:
if os.path.exists(newpath + '\\pdf'):
print(os.path.join(filepath), file)
copy(os.path.join(filepath, file), os.path.join(newpath + '\\pdf', file))
else:
print('pdf folder is not exitsed and create folder')
os.mkdir(newpath + '\\pdf')
copy(os.path.join(filepath, file), os.path.join(newpath + '\\pdf', file))
if 'docx' in file:
if os.path.exists(newpath+'\\docx'):
print(os.path.join(filepath), file)
copy(os.path.join(filepath,file), os.path.join(newpath+'\\docx',file))
else:
print('docx folder is not exitsed and create folder')
os.mkdir(newpath+'\\docx')
copy(os.path.join(filepath, file), os.path.join(newpath + '\\docx', file))
if __name__=='__main__':
path=r'C:\Users\fengz\Downloads'
newpath=r'C:\Users\fengz\Desktop\5.24_copy'
copyFile(path,newpath)
以上代码已做成函数,只需要输入path 和newpath 即可,可以用move 做剪切操作!