How does Python classify files according to suffixes
This article introduces the relevant knowledge of "how to classify Python according to file suffixes". 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!
For the files shown in the following figure, we classify the files by the suffix of the file name
Using os and shutil
First import the corresponding module and define the target folder and the folder to which it is classified.
Import osimport shutilsrc_folder ='E:\ documents\ Files to be sorted 'tar_folder =' E:\ documents\ classified Files' read all files under the destination folder files = os.listdir (src_folder) get the full path of each file for file in files: # splice the full path of each file src_path = src_folder +'\\'+ file print (src_path)
File display after grouping
Get the folder path after grouping for file in files: # splice the full path of each file src_path = src_folder +'\\'+ file if os.path.isfile (src_path): # the file path after moving # split the file people by point into the last bit, that is, the path of the target tar_path = tar_folder +' \'+ file.split ('.') [- 1] print (tar_path)
Move files to destination folder # create if not os.path.exists (tar_path): os.mkdir (tar_path) # move files shutil.move (src_path) if the folder does not exist Tar_path) complete code import osimport shutilsrc_folder ='E:\ document\ File to be classified 'tar_folder =' E:\ document\ classified file 'files = os.listdir (src_folder) for file in files: # piece together the full path of each file src_path = src_folder +'\\'+ file if os.path.isfile (src_path): # text after moving Part path # split the file people by point into the last bit that is the target path tar_path = tar_folder +'\\'+ file.split ('.) [- 1] # create if not os.path.exists (tar_path): os.mkdir (tar_path) # move the file shutil.move (src_path) if the folder does not exist Tar_path) use pathlib to read files under a folder using Path
Use Path () to create a path object and use glob to get all the files under the file
From pathlib import Pathsrc_folder = Path ('E:\ document\ files to be classified') tar_folder = Path ('E:\ document\ classified file') files = src_folder.glob ('*') for file in files: print (file.name) get the destination folder
Use the destination folder stitching file suffix,'/ 'can be used as a post-Path for stitching, and file.suffix can get the file name save suffix, .strip ('.') Get the file suffix name
For file in files: if file.is_file (): tar_path = tar_folder / file.suffix.strip ('.') Print (tar_path)
Mobile file if not tar_path.exists (): tar_path.mkdir (parents=True) file.replace (tar_path / file.name) "how Python classifies according to file suffixes" ends here. 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!