- shutil 、os模块
shutil.move 移动文件
shutil.copy 复制文件
os.mkdir 创建文件夹
- os.path.exists()
判断括号里的文件是否存在,括号内的可以是文件路径,存在返回True,不在返回False
import os
import shutil
path = r'F:\Desktop'
#for filepath,folders,files in walk(path):
for file in os.listdir(path):
if '.jpg' in file:
print(os.path.join(path,file))
shutil.move(os.path.join(path,file),r'F:\Desktop\图片')
elif '.pdf' in file:
if os.path.exists(r'F:\Desktop\pdf'):
print(os.path.join(path, file))
shutil.move(os.path.join(path,file),r'F:\Desktop\pdf')
else:
print('pdf文件夹不存在')
os.mkdir(r'F:\Desktop\pdf')
shutil.move(os.path.join(path, file), r'F:\Desktop\pdf')