preface

I believe that many little sister or little brother should have encountered and our receptionist little sister similar problems! I used to put the downloaded things on the desktop or in the “download” folder. After using it, I did not care about it. Over time, the folder became messy and it was very painful to reorganize

Since the little sister so sincere let me do a little favor, in general, if is the product manager is directly I refuse, but see little sister that looks at the front desk, refused to two words I couldn’t say, and little sister himself under the surface, it must be, the average person can’t eat technology will share with you, the face of the little sister can I selfishly ha! Take a look at the effect before automatic finishing:

After automatic sorting:

The effect is very good, much more comfortable!

1. Prepare

All you need to do is change the parameters of the call to auto_ORGANIZE in the main source code. For example, I want to organize the C:\Users\83493\Downloads folder:

if __name__ == "__main__":
    auto_organize(r"C:\Users\83493\Downloads")
Copy the code

As shown above, change the folder address on line 61 of the code. Run the script after the modification is complete:

python auto_files.py
Copy the code

Can instantly complete the sorting of the specified folder, extremely convenient

Principle 2.

The principle of this code is very simple. To organize these files, we traverse the currently specified folder: skip if it is a folder, detect the suffix if it is a file, and sort it into the appropriate folder

The folder names and their corresponding file suffixes are as follows:

DIRECTORIES = {" image ": ["," JPG "jpeg", "tiff", ". GIF ", ". BMP ", "PNG", "combined", "SVG," "heif", "PSD"], "video" : [". Avi ", ". FLV ", ". WMV ", "mov", "mp4", "webm", "vob", "MNG", "qt", ". MPG ", "the mpeg", "3 gp," "MKV"], "document" : [".oxps", ".epub", ".pages", ".docx", ".doc", ".fdf", ".ods", ".odt", ".pwi", ".xsn", ".xps", ".dotx", ".docm", ".dox", ". RVG ", ". The RTF ", "RTFD", ". WPD ", ". XLS "and". XLSX "and" powerpoint presentation ", "PPTX", "CSV",, "PDF", "zip file: [" a ", "ar", "cpio", "iso", "tar", ". Gz "and" fact "that". 7 z ", ". DMG ", "rar", "xar", "zip"], "video" : [".aac", ".aa", ".aac", ".dvf", ".m4a", ".m4b", ".m4p", ".mp3", ".msv", "ogg", "oga", ".raw", ".vox", ".wav", ".wma"], "Text" : [". TXT ", "in" and "out"], "programming" : [". Py ", "it", "HTML", ". HTM ", "XHTML", "c", "CPP", "Java" and "CSS"], "executable" : [".exe"], }Copy the code

Next comes the auto-collation part of the code, first traverses the specified folder, identifies the suffix and categorizes it into the corresponding folder

for entry in os.scandir(dirval): if entry.is_dir(): Continue file_path = Path(dirval + "\\" + entry.name) file_format = file_path.suffix.lower() If file_format in FILE_FORMATS: directory_path = Path(dirval + "\\" + FILE_FORMATS[file_format]) directory_path.mkdir(exist_ok=True) file_path.rename(directory_path.joinpath(entry.name))Copy the code

Or the occurrence of suffixes that don’t exist in our dictionaries, grouped into folders called other files:

Except: pass for dir in os.scandir(dirval): try: if dir.is_dir(): # delete empty folder os.rmdir(dir) else: Temp = STR (Path (dir)). The split (' \ \ ') # segmentation Path to the file Path = '\ \'. Join (temp] [: - 1) print (Path + 'other documents \ \ \ \' + STR (temp [1])) OS. Rename (STR (Path (dir)), the Path + 'other documents \ \ \ \' + STR (temp) [1]) except: passCopy the code

3. Customize collation

If you don’t want the DIRECTORIES set up, you want to customize the DIRECTORIES for files with specific suffixes in another folder, such as.py files in the Python scripts folder, you can change the DIRECTORIES variable like this:

Before the changes:

DIRECTORIES = { # ...... "Programming" : [". Py ", "it", "HTML", ". HTM ", "XHTML", "c", "CPP", "Java" and "CSS"], #... }Copy the code

After the changes:

DIRECTORIES = { # ...... "Programming" : [". HTML 5 ", "HTML", ". HTM ", "XHTML", "c", "CPP", "Java" and "CSS"], "Python script:" [". Py "], #... }Copy the code

You can use this method to customize the file formats that you want to organize

What if you only want DIRECTORIES with suffixes that are in the DIRECTORIES variable to be sorted, but not those that are not, and don’t need to be placed in the ‘Other files’ directory?

To do this, simply delete the following sections of code:

#... Except: pass for dir in os.scandir(dirval): try: if dir.is_dir(): # delete empty folder os.rmdir(dir) else: Temp = STR (Path (dir)). The split (' \ \ ') # segmentation Path to the file Path = '\ \'. Join (temp] [: - 1) print (Path + 'other documents \ \ \ \' + STR (temp [1])) OS. Rename (STR (Path (dir)), the Path + 'other documents \ \ \ \' + STR (temp) [1]) except: pass #...Copy the code

How about it? Is it convenient? If this simple script helps you, please forward it to someone who needs it

Good not much and you BB, I want to go home with little sister to eat noodles! Finally asked for the little sister’s consent, put the photos below everyone enjoy it!

Do I have to get off the hook?! Gnome male – “!

Requiring scripts or source code:

① Over 3000 Python ebooks are available

The Python development environment installation tutorial is available

③Python400 self-study video is available

(4) The common vocabulary of software development is

⑤ The Python learning roadmap has

⑥ Project source code cases to share

If you use it, you can take it away directly, in my QQ technical exchange group group number: 754370353 (pure technical exchange and resource sharing, advertising is not allowed) to take it away by yourself

Click here to collect