Home >Backend Development >Python Tutorial >How to implement file classifier based on Python
By customizing the file directory that needs to be sorted, all files under the directory can be classified according to the file format.
The Python technology stack used to implement logic is the comprehensive use of the three standard libraries of os, glob, and shutil to complete automated file sorting.
Import these three file processing modules into the code block respectively and enter the subsequent development operations.
# It imports the os module. import os # Shutil is a module that provides a number of high-level operations on files and collections of files. import shutil # The glob module finds all the pathnames matching a specified pattern according to the rules used by the Unix shell, # although results are returned in arbitrary order. No tilde expansion is done, but *, ?, and character ranges expressed # with [] will be correctly matched. import glob import sys
Set the uncatched_dir of the file directory that needs to be classified and the target_dir of the classified file storage directory so that they can be entered manually.
# Asking the user to input the path of the directory that contains the files to be sorted. uncatched_dir = input('请输入待分类的文件路径:\n') # It checks if the uncatched_dir is empty. if uncatched_dir.strip() == '': print('待分类的文件夹路径不能为空!') sys.exit() # Asking the user to input the path of the directory that contains the files to be sorted. target_dir = input('请输入分类后文件存放的目标路径:\n') # It checks if the target_dir is empty. if target_dir.strip() == '': print('分类后的文件存放路径不能为空!') sys.exit()
Check whether the entered file storage directory path after classification exists, because it is likely to enter a new path, and if it does not exist, create a new path.
# It checks if the target_dir exists. If it does not exist, it creates a new directory in the current working directory. if not os.path.exists(target_dir): # It creates a new directory in the current working directory. os.mkdir(target_dir)
Define a variable file_move_num for the number of file moves, and a variable dir_new_num for the number of newly created folders to record the results of file sorting.
# A variable that is used to count the number of files that have been moved. file_move_num = 0 # A variable that is used to count the number of new directories that have been created. dir_new_num = 0
Traverse the folder directory uncatched_dir that needs to be sorted, and automatically sort all types of files under the directory.
# A for loop that iterates through all the files in the uncatched_dir directory. for file_ in glob.glob(f'{uncatched_dir}/**/*', recursive=True): # It checks if the file is a file. if os.path.isfile(file_): # It gets the file name of the file. file_name = os.path.basename(file_) # Checking if the file name contains a period. if '.' in file_name: # Getting the suffix of the file. suffix_name = file_name.split('.')[-1] else: # Used to classify files that do not have a suffix. suffix_name = 'others' # It checks if the directory exists. If it does not exist, it creates a new directory in the current working # directory. if not os.path.exists(f'{target_dir}/{suffix_name}'): # It creates a new directory in the current working directory. os.mkdir(f'{target_dir}/{suffix_name}') # Adding 1 to the variable dir_new_num. dir_new_num += 1 # It copies the file to the target directory. shutil.copy(file_, f'{target_dir}/{suffix_name}') # Adding 1 to the variable file_move_num. file_move_num += 1
Note: In order to avoid exceptions caused by moving folders, especially system disks, copying is used here, which is the shutil.copy function.
Finally, just use the print function to print the number of file categories and the number of new folders.
print(f'整理完成,有{file_move_num}个文件分类到了{dir_new_num}个文件夹中!\n') input('输入任意键关闭窗口...')
In order to avoid closing the command window directly after the program execution is completed, the input function is used above to maintain the effect of the window being suspended.
The above is the detailed content of How to implement file classifier based on Python. For more information, please follow other related articles on the PHP Chinese website!