In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to organize documents automatically by python". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Demo effect:
Before use
After use
Code:
# #-*-coding:utf-8-*-import osimport globimport shutilimport tkinterimport tkinter.filedialogfrom datetime import datetimedef start (): root = tkinter.Tk () root.withdraw () dirname = tkinter.filedialog.askdirectory (parent=root,initialdir= "/", title=' Please select a folder') return dirname# defines a file dictionary with different file types Belong to different folders file_dict = {"picture": ["jpeg", "jpg", "tiff", "gif", "bmp", "png", "bpg", "svg", "heif", "psd"], "video": ["avi", "flv", "wmv", "mov", "mp4", "webm", "vob", "mng", "qt", "mpg" "mpeg", "3gp", "mkv"], "audio": ["aac", "aa", "aac", "dvf", "M4a", "M4b", "M4P", "mp3", "msv", "ogg", "oga", "raw", "vox", "wav", "wma"], "document": ["oxps", "epub", "pages", "docx" "doc", "fdf", "ods", "odt", "pwi", "xsn", "xps", "dotx", "docm", "dox", "rvg", "rtf", "rtfd", "wpd", "xls", "xlsx", "xlsm", "ppt", "pptx", "csv", "pdf", "md", "xmind"], "zip file": [a "," ar " "cpio", "iso", "tar", "gz", "rz", "7z", "dmg", "rar", "xar", "zip"], "text": ["txt", "in", "out", "json", "xml", "log"], "program script": ["py", "html5", "html", "htm", "xhtml", "cpp", "java" "css", "sql", "executable program": ['exe',' bat', 'lnk',' sys', 'com','apk'],' font file': ['eot',' otf', 'fon',' font', 'ttf',' ttc', 'woff',' woff2','shx'], 'drawing file': ['bak','dwg','dxf'' 'dwl','dwl2','stp','SLDPRT','ipj','ipt','idw']} # define a function Pass in the suffix corresponding to each file. Determine whether the file exists in the dictionary file_dict; # if it exists, return the corresponding folder name; if not, name the folder "unknown category" Def JudgeFile (suffix): for name, type_list in file_dict.items (): if suffix.lower () in type_list: return name return "unknown classification" if _ _ name__ = ='_ main__': try: while True: path = start () print ("--> path is:" Path) if path = "": print ("No path selected!") Break # recursively gets all files and folders under the pending File path. StartTime = datetime.now () .second for file in glob.glob (f "{path} / * * / *", recursive=True): # since we are classifying files, we need to pick out the files here. If os.path.isfile (file): # because of the isfile () function, you get the full path of each file. Here, call the basename () function to get the file name directly; file_name = os.path.basename (file) suffix = file_name.split (".") [- 1] # to determine whether the "file name" is in the dictionary. Name = JudgeFile (suffix) # create corresponding folders based on the classification of each file. If not os.path.exists (f "{path}\ {name}"): os.mkdir (f "{path}\ {name}") print ('path-- >', name) # copies the files to their respective folders. # shutil.copy (file, f "{path}\ {name}") # move the files to their respective folders. Shutil.move (file, f "{path}\ {name}") endTime = datetime.now () .second countTime= endTime-startTime print ("- > has been sorted out. Total cost {} s ".format (countTime) a = input ('- > Please press enter to exit:) if a =': break except BaseException: print ('duplicate files!')
It is easy to execute, as long as you finish the program, click the program to run, wait for the pop-up window, and select the folder that needs to be sorted out.
If you think the above code is complex, you can try the following simpler program.
How to achieve automatic file classification?
There are many different types of resource conditions in the same directory
1. classification
two。 Create a classified catalog
3. Move file resources
Import osimport shutilimport tkinterimport tkinter.filedialogfrom datetime import datetimedef start (): root = tkinter.Tk () root.withdraw () dirname = tkinter.filedialog.askdirectory (parent=root,initialdir= "/ Title=' Please select folder') return dirname# source file existence path src_dir=start () # Classification resource existence path dest_dir=src_dir# determine whether the directory exists if not os.path.exists (dest_dir): os.mkdir (dest_dir) # Source directory analysis files=os.listdir (src_dir) for item in files: src_path=os.path.join (src_dir) Item) # determine status if os.path.isfile (src_path): # if it is a file Enter the code block # determine the type of file resource ndir = item.split ('.') [- 1] desc_path=os.path.join (dest_dir,ndir) # create a classification directory if not os.path.exists (desc_path): # if the classification subdirectory does not exist This is the end of creating os.mkdir (desc_path) shutil.move (src_path,desc_path) "how to organize files automatically with python". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.