Python一键自动化整理文件,任意路径下文件,都给你整理的明明白白!
留言有礼,每天都会从留言中选出三位抽取6.6元红包
作者:Huny
关注下方作者博客园,精彩抢先看!
https://www.cnblogs.com/huny/p/14146719.html
1. 前言
2. 基础版
import os
import glob
import shutil
'''
@Author: huny
@date: 2020.12.06
@function: 文件整理
'''
class FileType():
def __init__(self):
self.filetype = {
'图片': ['.jpeg', '.jpg', '.tiff', '.gif', '.bmp', '.png', '.bpg', 'svg', '.heif', '.psd'],
'视频': ['.avi', '.flv', '.wmv', '.mov', '.mp4', '.webm', '.vob', '.mng', '.qt', '.mpg', '.mpeg', '.3gp', '.mkv'],
'音频': ['.aac', '.aa', '.aac', '.dvf', '.m4a', '.m4b', '.m4p', '.mp3', '.msv', '.ogg', '.oga', '.raw', '.vox', '.wav', '.wma'],
'文档': ['.oxps', '.epub', '.pages', '.docx', '.doc', '.fdf', '.ods', '.odt', '.pwi', '.xsn', '.xps', '.dotx', '.docm', '.dox',
'.rvg', '.rtf', '.rtfd', '.wpd', '.xls', '.xlsx', '.ppt', '.pptx', '.csv', '.pdf', '.md','.xmind'],
'压缩文件': ['.a', '.ar', '.cpio', '.iso', '.tar', '.gz', '.rz', '.7z', '.dmg', '.rar', '.xar', '.zip'],
'文本': ['.txt', '.in', '.out', '.json','xml','.log'],
'程序脚本': ['.py', '.html5', '.html', '.htm', '.xhtml', '.c', '.cpp', '.java', '.css','.sql'],
'可执行程序': ['.exe','.bat','.lnk'],
'字体文件': ['.ttf', '.OTF', '.WOFF', '.EOT']
}
def JudgeFile(self, pathname):
for name, type in self.filetype.items():
if pathname in type:
return name
return '无法判断类型文件'
class DeskTopOrg(object):
def __init__(self):
self.filetype = FileType()
def Organization(self):
filepath = os.path.join(os.path.expanduser('~'),'Desktop')
paths = glob.glob(filepath + '/*.*')
print('paths-->',paths)
for path in paths:
try:
if not os.path.isdir(path):
file = os.path.splitext(path)
filename,type = file
print('type-->',type)
print('filename-->',filename)
print('path-->',path)
dir_path = os.path.dirname(path)
print('dir_path-->',dir_path)
savePath = dir_path + '/{}'.format(self.filetype.JudgeFile(type))
print('savePath-->',savePath)
if not os.path.exists(savePath):
os.mkdir(savePath)
shutil.move(path, savePath)
else:
shutil.move(path, savePath)
except FileNotFoundError:
pass
print('程序执行结束!')
if __name__ == '__main__':
try:
while True:
desktopOrg = DeskTopOrg()
desktopOrg.Organization()
print('---->你的文件已经整理完成。')
a = input('---->请按回车键退出:')
if a == '':
break
except BaseException:
print('ERROE:路径错误或有重复的文档')
3. 升级版
import glob
import shutil
'''
@Author: huny
@date: 2020.12.06
@function: 文件整理
'''
class FileType():
def __init__(self):
self.filetype = {
'图片': ['.jpeg', '.jpg', '.tiff', '.gif', '.bmp', '.png', '.bpg', 'svg', '.heif', '.psd'],
'视频': ['.avi', '.flv', '.wmv', '.mov', '.mp4', '.webm', '.vob', '.mng', '.qt', '.mpg', '.mpeg', '.3gp', '.mkv'],
'音频': ['.aac', '.aa', '.aac', '.dvf', '.m4a', '.m4b', '.m4p', '.mp3', '.msv', '.ogg', '.oga', '.raw', '.vox', '.wav', '.wma'],
'文档': ['.oxps', '.epub', '.pages', '.docx', '.doc', '.fdf', '.ods', '.odt', '.pwi', '.xsn', '.xps', '.dotx', '.docm', '.dox',
'.rvg', '.rtf', '.rtfd', '.wpd', '.xls', '.xlsx', '.ppt', '.pptx', '.csv', '.pdf', '.md','.xmind'],
'压缩文件': ['.a', '.ar', '.cpio', '.iso', '.tar', '.gz', '.rz', '.7z', '.dmg', '.rar', '.xar', '.zip'],
'文本': ['.txt', '.in', '.out', '.json','xml','.log'],
'程序脚本': ['.py', '.html5', '.html', '.htm', '.xhtml', '.c', '.cpp', '.java', '.css','.sql'],
'可执行程序': ['.exe','.bat','.lnk'],
'字体文件': ['.ttf', '.OTF', '.WOFF', '.EOT']
}
def JudgeFile(self, pathname):
for name, type in self.filetype.items():
if pathname in type:
return name
return '无法判断类型文件'
class DeskTopOrg(object):
def __init__(self):
self.filetype = FileType()
def Organization(self):
filepath = input('请输入需要整理的文件夹路径: ')
paths = glob.glob(filepath + '/*.*')
print('paths-->',paths)
for path in paths:
try:
if not os.path.isdir(path):
file = os.path.splitext(path)
filename,type = file
print('type-->',type)
print('filename-->',filename)
print('path-->',path)
dir_path = os.path.dirname(path)
print('dir_path-->',dir_path)
savePath = dir_path + '/{}'.format(self.filetype.JudgeFile(type))
print('savePath-->',savePath)
if not os.path.exists(savePath):
os.mkdir(savePath)
shutil.move(path, savePath)
else:
shutil.move(path, savePath)
except FileNotFoundError:
pass
print('程序执行结束!')
if __name__ == '__main__':
try:
while True:
desktopOrg = DeskTopOrg()
desktopOrg.Organization()
print('---->你的文件已经整理完成。')
a = input('---->请按回车键退出:')
if a == '':
break
except BaseException:
print('ERROE:路径错误或有重复的文档')
4. 打包成exe可执行文件
① 安装pyinstaller
② 执行打包命令
pyinstaller -F ***.py
5. 活动提醒
不要忘记关注我们Python客栈,11-16号每天都会抽取100份红包,大家快来参与吧!!!
今日关键词是幸福
01
活动时间
2月11日~2月16日
02
参与方式
在公众号主页对话框发送关键词【幸福】,即可参与新年祝福活动!
▲举个栗子
03
活动规则
(1)活动期间,每个账号每天都可参与抽奖