鱼C论坛

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

[原创] 作业:文件整理

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

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

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

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() 大家有没有更好的方法呢?

文件整理.zip

782 Bytes, 下载次数: 2

本帖被以下淘专辑推荐:

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-7-11 03:12

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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