Fundamentals 4 min read

Python Script for Automatic Desktop and Folder Organization by File Type

This article demonstrates how to use a Python script to automatically categorize and move files on the desktop or in any folder into type‑based subfolders by defining custom file‑extension groups, scanning directories, creating target folders, and moving matching files, with complete code examples and execution instructions.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Python Script for Automatic Desktop and Folder Organization by File Type

The article introduces a Python script that helps clean up a cluttered desktop or folder by automatically sorting files into categorized subfolders based on their extensions.

First, a dictionary FILE_FORMAT is defined to map human‑readable categories (e.g., "图片", "文档", "视频") to lists of file extensions such as .jpg , .docx , .mp4 , etc.

FILE_FORMAT = {
    "图片": [".jpg", ".jpeg", ".bpm", ".png", ".gif"],
    "文档": [".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".pdf", ".txt", ".md"],
    "视频": [".mp4", "avi", "wmv"],
    "音频": [".mp3"],
    "压缩": [".rar", ".zip", ".tar", ".gz", ".7z", "bz"],
    "脚本": [".ps1", ".sh", ".bat", ".py"],
    "可执行文件": [".exe", ".msi"],
    "网页文件": [".html", ".xml", ".mhtml"],
    "快捷方式": [".lnk"]
}

Next, the script defines the target directory to be organized (e.g., orgPath = 'D:\\direct' ) and iterates over each entry using os.scandir . It skips sub‑directories, prints file names, and determines each file’s suffix.

# Define the folder to organize
orgPath = 'D:\\direct'
print(os.scandir(orgPath))
for myfile in os.scandir(orgPath):
    if myfile.is_dir():
        print('%s是文件夹' % myfile)
        continue
    print(myfile.name)

For each file, the script checks whether its lower‑cased suffix matches any extension in the predefined categories. If a match is found, it creates the corresponding category folder (if it does not already exist) and moves the file into it.

# Locate the file path
file_path = Path(orgPath + '\\' + myfile.name)
lower_file_path = file_path.suffix.lower()
for formt in FILE_FORMAT:
    if lower_file_path in FILE_FORMAT[formt]:
        directory_path = Path(orgPath + '\\' + formt)
        directory_path.mkdir(exist_ok=True)
        file_path.rename(directory_path.joinpath(myfile.name))
        print('文件整理已完成!')

To run the script, simply execute it with Python from the command line, e.g., python file_org.py , after adjusting the orgPath to point to your own directory.

The article also includes screenshots of the before‑and‑after results and promotional material for a free Python learning course, but the core instructional content focuses on the file‑organization script.

AutomationscriptingFilesystemfile organization
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.