鱼C论坛

 找回密码
 立即注册
查看: 1476|回复: 0

[原创] 作业:文件整理

[复制链接]
发表于 2019-1-23 22:29:39 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 惊悚boy 于 2019-1-27 23:16 编辑

此脚本可以将当前文件夹内的视频音频图片文档自动归类到各自类型的文件夹中
  1. import os,shutil

  2. print('------请放置在需要整理的目录下------')
  3. confirm = input('是否确认(Y/N)')
  4. if confirm == Y:
  5.     os.chdir(os.curdir)
  6. else:
  7.     os.exit()

  8. video_ext = ['.mp4','.avi','rmvb','flv']
  9. audio_ext = ['.mp3','.wav']
  10. doc_ext = ['.doc','.txt','.xls','.ppt','.docx','.pptx','.xlsx']
  11. pic_ext = ['.jpg','.bmp','.gif','.png']

  12. video_list = []
  13. audio_list =[]
  14. doc_list = []
  15. pic_list = []
  16. fileType = ''

  17. def analyze_file(each_file):
  18.     if os.path.isfile(each_file):
  19.         ext = os.path.splitext(each_file)
  20.         if ext[1] in video_ext:
  21.             video_list.append(os.curdir + os.sep + each_file)
  22.         elif ext[1] in audio_ext:
  23.             audio_list.append(os.curdir + os.sep + each_file)
  24.         elif ext[1] in doc_ext:
  25.             doc_list.append(os.curdir + os.sep + each_file)
  26.         elif ext[1] in pic_ext:
  27.             pic_list.append(os.curdir + os.sep + each_file)

  28. def move_file(file_list,fileType):
  29.     for each_file in file_list:
  30.         try:
  31.             os.mkdir(os.curdir + os.sep + fileType)
  32.         except OSError:
  33.             shutil.move(each_file,os.curdir + os.sep + fileType)

  34. all_file = os.listdir(os.curdir)
  35. for each_file in all_file:
  36.     analyze_file(each_file)
  37. fileType = '视频'
  38. move_file(video_list,fileType)
  39. fileType = '音频'
  40. move_file(audio_list,fileType)
  41. fileType = '文档'
  42. move_file(doc_list,fileType)
  43. fileType = '图像'
  44. move_file(pic_list,fileType)

  45. print('整理以完成')
复制代码


希望大家提建议,总觉得第一个函数写的有点繁琐,fileType总要重新赋值,一遍一遍调用move_file() 大家有没有更好的方法呢?

文件整理.zip

782 Bytes, 下载次数: 2

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-6-8 07:55

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表