Sixty lines of python code to complete a file classifier

Whether your desktop is full of various files like this, but each file is not clear whether it has any function behind it, and you dare not delete it. It is too troublesome to transfer one by one. It doesn't matter. Today, I will take you to build a file categorizer with python. You can categorize with one click. You will never see a full desktop again.

Libraries we need to import three libraries: os, shutil and glob. os is used to process paths, shutil is used to delete copies of files, and glob is used to traverse files in specified folders.

# -*- coding:utf-8 -*-#author: novice Xiaobai's learning sharing import osimport shutilimport glo

Parameter acquisition program then we write a parameter acquisition function to process the user input of parameters such as path and storage path for file classification.

# -*- coding:utf-8 -*-#author: novice Xiaobai's learning and sharing def getconf(): Flag = "1" print ("enter '1' to organize desktop folders, please enter the full path for other folders") path=input("please enter the folder to be sorted:") if path== "1": goal_ Path = os Path Join (os.path.expanduser ('~'), "desktop") else: goal_ Path = path flag = input ("whether the original file needs to be retained (this configuration is only valid for non desktop folders), please enter '1' for retention and '2': 'for not retention) mkdir_path=input("please input the storage path of the collated file:") if not os Path Exists (mkdir_path): os MKDIR (mkdir_path) return goal_ Path, MKDIR_ Path, flag

File processing program because desktop folders are different from ordinary folders, there are a large number of executable programs and shortcuts on desktop files. We do not need to classify this part of the files. So I separate the desktop folder from the normal folder. Let's first look at how normal folders are handled.

# -*- coding:utf-8 -*-#author: novice Xiaobai's learning and sharing def filehandle (goal\u path, mkdir\u path, flag): file_ Num = 0 path_ Num = 0 for file in glob Glob (f'{goal\u path}/**/*', recursive=true): if os Path Isfile (file): filename = os Path Basename (file) if '.' In filename: suffix = filename Split ('.') [-1] else: suffix ='others' if not os Path Exists (f'{mkdir_path}/{suffix}'): os MKDIR (f'{mkdir_path}/{suffix}') path_ Num += 1 shutil Copy (file, f'{mkdir_path}/{suffix}') file_ Num += 1 if flag== "2": shutil Rmtree (goal_path) return file_ Num, path_ Num

After the ordinary folder is processed, we judge whether the original file needs to be retained according to the flag parameter entered. If not, we will delete the contents of the target folder and all its subordinate files.

# -*- coding:utf-8 -*-#author: novice Xiaobai's learning sharing if flag=="2": shutil Rmtree (goal_path)

Let's look at how desktop files are handled

# -*- coding:utf-8 -*-#author: novice Xiaobai's learning sharing def desktophandle (goal\u path, mkdir\u path): file_ Num = 0 path_ Num = 0 for file in glob Glob (f'{goal\u path}/**/*', recursive=true): if os Path Isfile (file): filename = os Path Basename (file) if '.' In filename: suffix = filename Split ('.') [-1] else: suffix ='others' if suffix== "exe" or suffix== "LNK": pass else: if not os Path Exists (f'{mkdir_path}/{suffix}'): os MKDIR (f'{mkdir_path}/{suffix}') path_ Num += 1 shutil Copy (file, f'{mkdir_path}/{suffix}') file_ Num + = 1 return file_ Num, path_ Num

The general idea is the same as that of ordinary folders, but in the processing of desktop files, we filter the files with exe and lnk suffixes, do not copy them, and we copy other files to the target folder.

 # -*- coding:utf-8 -*-#author: novice Xiaobai's learning sharing if suffix== "exe" or suffix== "LNK": pass else: if not os Path Exists (f'{mkdir_path}/{suffix}'): os MKDIR (f'{mkdir_path}/{suffix}') path_ Num += 1 shutil Copy (file, f'{mkdir_path}/{suffix}')

In addition, I haven't put the operation of deleting the original file in the desktop file processing. If you need a partner, you can directly add the operation of deleting the file.

Statistical information program

After all the files have been processed, I also made a statistical program to count how many files have been processed and how many classified folders have been created.

# -*- coding:utf-8 -*-#author: novice Xiaobai's learning and sharing def writeinfo (mkdir_path): file_ Name = MKDIR_ Path+ "\info.txt" with open (file\u name,'w', encoding='utf-8') as f: f.write

main program

Finally, it is our main program. You can call the parameter acquisition program, file processing program and statistical program respectively.

# -*- coding:utf-8 -*-#author: novice Xiaobai's learning sharing if__ Name__ = = '__ Main__': Goal_ Path, MKDIR_ Path, flag=getconf() if goal_ Path==os Path Join (os.path.expanduser ('~'), "desktop"): file_ Num, path_ Num=desktophandle (goal\u path, mkdir\u path) else: file_ Num, path_ Num=filehandle (goal\u path, mkdir\u path, flag) writeinfo (mkdir\u path)

Achieve results

Well, the whole program has been analyzed. Finally, let's see the final implementation effect.

The execution of the program is as follows

Classification of desktop files

Classification of ordinary documents

finish

Small partners who need the program source code can get the download link of the source code by following the official account "rookie Xiaobai's learning and sharing" and replying to "file classifier source code".

Small partners who need program execution files can follow the official account "rookie Xiaobai's learning and sharing" and reply to "file classifier executable" to obtain the download link of the executable program packaged by rookie Xiaobai.

OK, that's all for today's sharing. If you also agree with rookie Xiaobai, then give her attention, watching, praise and appreciation. Your support is my continuous motivation.

Tags: Python

Posted by tom2oo8 on Mon, 30 May 2022 12:34:36 +0530