圆月山庄资源网 Design By www.vgjia.com
前言
工作上的文档和资料好几个月没整理了,因为平常太忙都是随手往桌面丢。整个桌面杂乱无章全是文档和资料。几乎快占满整个屏幕了,所有我必须要整理一下了。但是手动整理太费时间了,于是我想到了python。
示例
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",s".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:路径错误或有重复的文档")
整理完了,桌面清爽了不少。(注意此脚本是按后缀进行分类归档的)
进阶
基于这个我想是否可以对其他不同的路径进行整理呢,于是又优化了一下
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 = 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:路径错误或有重复的文档")
可以自由的整理你想要整理的路径。
后续
其他朋友也有需求,但是又没有python环境,于是我将程序打包成exe执行文件。
安装pyinstaller
pip install pyinstaller
执行打包命令
#在程序脚本的路径执行以下命令 pyinstaller -F ***.py
执行完后生成几个文件,在dist文件下有一个exe可执行文件,将他单独发给朋友即可。
别忘了先自己测试一遍。
以上就是Python实现自动整理文件的脚本的详细内容,更多关于python 自动整理文件的资料请关注其它相关文章!
圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com
暂无评论...
更新日志
2024年11月01日
2024年11月01日
- 群星《戏梦》[320K/MP3][119.93MB]
- 群星《戏梦》[FLAC/分轨][308.4MB]
- 魔兽世界永久60级什么时间开 永久60级开启时间介绍
- 魔兽世界奥卡兹岛血藻刷新点一览 wlk奥卡兹岛血藻刷新位置介绍
- 英雄联盟s14中国队种子怎么排名 s14中国队种子队伍排名一览
- 柏菲·梁玉嵘《知音梁品》限量开盘母带ORMCD[低速原抓WAV+CUE]
- [东升唱片]孙露《寂寞诱惑AQCD》高码率[FLAC]
- 群星-第8届2012十大发烧唱片精选HQ2CD[WAV+CUE]
- 九号公司两轮电动安全季行动圆满收官:为行业树立安全管理新标杆
- 创新驱动未来 中国移动5G云游戏深化智能运维应用推动行业发展
- 大型手游“帧数拉满” 骁龙8至尊版游戏表现强悍
- 谢金燕.2002-YOYO姊妹【华特】【WAV+CUE】
- 群星.1994-摇滚列车【音乐家】【WAV+CUE】
- 陈艾湄.1996-牵绊【巨翼】【WAV+CUE】
- 群星《无法抗拒的谎言 电视剧原声带》[320K/MP3][69.98MB]