這期內容當中小編將會給大家?guī)碛嘘P利用Python怎么編寫一個自動整理文件的腳本,文章內容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
創(chuàng)新互聯建站長期為上1000家客戶提供的網站建設服務,團隊從業(yè)經驗10年,關注不同地域、不同群體,并針對不同對象提供差異化的產品和服務;打造開放共贏平臺,與合作伙伴共同營造健康的互聯網生態(tài)環(huán)境。為白堿灘企業(yè)提供專業(yè)的成都網站建設、網站制作,白堿灘網站改版等技術服務。擁有10余年豐富建站經驗和眾多成功案例,為您定制開發(fā)。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"], "可執(zhí)行程序": [".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("程序執(zhí)行結束!") if __name__ == '__main__': try: while True: desktopOrg = DeskTopOrg() desktopOrg.Organization() print("---->你的桌面已經整理完成。") a = input('---->請按回車鍵退出:') if a == '': break except BaseException: print("ERROE:路徑錯誤或有重復的文檔")
整理完了,桌面清爽了不少。(注意此腳本是按后綴進行分類歸檔的)
基于這個我想是否可以對其他不同的路徑進行整理呢,于是又優(yōu)化了一下
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"], "可執(zhí)行程序": [".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("程序執(zhí)行結束!") if __name__ == '__main__': try: while True: desktopOrg = DeskTopOrg() desktopOrg.Organization() print("---->你的文件已經整理完成。") a = input('---->請按回車鍵退出:') if a == '': break except BaseException: print("ERROE:路徑錯誤或有重復的文檔")
可以自由的整理你想要整理的路徑。
其他朋友也有需求,但是又沒有python環(huán)境,于是我將程序打包成exe執(zhí)行文件。
安裝pyinstaller
pip install pyinstaller
執(zhí)行打包命令
#在程序腳本的路徑執(zhí)行以下命令 pyinstaller -F ***.py
執(zhí)行完后生成幾個文件,在dist文件下有一個exe可執(zhí)行文件,將他單獨發(fā)給朋友即可。
別忘了先自己測試一遍。
上述就是小編為大家分享的利用Python怎么編寫一個自動整理文件的腳本了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注創(chuàng)新互聯行業(yè)資訊頻道。