用于整理文件,比如:
我需要将这些文件按文件名整理,相似文件名的放入新建一个文件夹,统一放入文件夹中
import os
import shutil
current_path = input("请输入你需要整理的目录目录:")
print('当前目录:'+current_path)
filename_list = os.listdir(current_path)
print('当前目录下文件:',filename_list)
while True:
goal_name = input("请输入你需要整理的文件名:")
try:
os.mkdir(current_path + '\\' + goal_name)
print('创建文件夹' + goal_name)
except:
pass
for filename in filename_list:
try:
name1, name2 = filename.split('.')
if name2 == 'docx' or name2 == 'doc': # 根据不同的值修改不同的文件类型
if(goal_name in name1):
try:
shutil.move(current_path+'\\'+filename, current_path+'\\'+goal_name)
print(filename+'转移成功!')
except Exception as e:
print('移动失败:' + e)
except:
pass
print('整理完毕!')