python实现⽂件⾃动分类整理_利⽤Python怎么编写⼀个⾃动
整理⽂件的脚本
利⽤Python怎么编写⼀个⾃动整理⽂件的脚本
发布时间:2020-12-17 16:41:40
来源:亿速云
阅读:93
作者:Leah
这期内容当中⼩编将会给⼤家带来有关利⽤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(panduser('~'),"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 ists(savePath):
os.mkdir(savePath)
else:
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怎么读取桌面上的文件
进阶
基于这个我想是否可以对其他不同的路径进⾏整理呢,于是⼜优化了⼀下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 ists(savePath):
os.mkdir(savePath)
else:
except FileNotFoundError:
pass
print("程序执⾏结束!")
if __name__ == '__main__':
try:
while True:
desktopOrg = DeskTopOrg()
desktopOrg.Organization()
print("---->你的⽂件已经整理完成。")
a = input('---->请按回车键退出:')
if a == '':
break
except BaseException:
print("ERROE:路径错误或有重复的⽂档")
可以⾃由的整理你想要整理的路径。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论