|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 惊悚boy 于 2019-1-27 23:16 编辑
此脚本可以将当前文件夹内的视频音频图片文档自动归类到各自类型的文件夹中
- import os,shutil
- print('------请放置在需要整理的目录下------')
- confirm = input('是否确认(Y/N)')
- if confirm == Y:
- os.chdir(os.curdir)
- else:
- os.exit()
- video_ext = ['.mp4','.avi','rmvb','flv']
- audio_ext = ['.mp3','.wav']
- doc_ext = ['.doc','.txt','.xls','.ppt','.docx','.pptx','.xlsx']
- pic_ext = ['.jpg','.bmp','.gif','.png']
- video_list = []
- audio_list =[]
- doc_list = []
- pic_list = []
- fileType = ''
- def analyze_file(each_file):
- if os.path.isfile(each_file):
- ext = os.path.splitext(each_file)
- if ext[1] in video_ext:
- video_list.append(os.curdir + os.sep + each_file)
- elif ext[1] in audio_ext:
- audio_list.append(os.curdir + os.sep + each_file)
- elif ext[1] in doc_ext:
- doc_list.append(os.curdir + os.sep + each_file)
- elif ext[1] in pic_ext:
- pic_list.append(os.curdir + os.sep + each_file)
- def move_file(file_list,fileType):
- for each_file in file_list:
- try:
- os.mkdir(os.curdir + os.sep + fileType)
- except OSError:
- shutil.move(each_file,os.curdir + os.sep + fileType)
- all_file = os.listdir(os.curdir)
- for each_file in all_file:
- analyze_file(each_file)
- fileType = '视频'
- move_file(video_list,fileType)
- fileType = '音频'
- move_file(audio_list,fileType)
- fileType = '文档'
- move_file(doc_list,fileType)
- fileType = '图像'
- move_file(pic_list,fileType)
- print('整理以完成')
复制代码
希望大家提建议,总觉得第一个函数写的有点繁琐,fileType总要重新赋值,一遍一遍调用move_file() 大家有没有更好的方法呢? |
|