File Organizing with Python: Rename, Move, Copy & Delete Files and Folders

Ask questions Research chat →

https://patloeber.com/file-organizing-python?source=newsletter · scraped

python

Attachments

Scraped Content

— 518 words · 2026-02-14 17:38:20 UTC ·

Excerpt

![](https://prod-files-secure.s3.us-west-2.amazonaws.com/871f1661-80b8-4d0c-ac3b-2adfc6ff4c66/1022d66b-64fe-4c51-80a6-201e41228f5b/image?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=ASIAZI2LB46624BQZZUU%2F20260214%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20260214T173820Z&X-Amz-Expires=3600&X-Amz-Security-Token=IQoJb3JpZ2luX2VjEEEaCXVzLXdlc3QtMiJGMEQCIHlyC0SZ88a05iCRk2UUnGk3bqFwDokQfucQezc9dwKTAiAHdmZvdGWAY8MLAsM1yq7oYnO0UcWKY1jXUt0VbpN0zyr%2FAwgKEAAaDDYzNzQyMzE4MzgwNSIMxJS%2FCjNQEXBdPcSeKtwDiexq0qTZYEYTaYhww%2BFq5JwyxJxRGtVphJnDY2WI7IIx9%2BYC6pYsuPob4zguzeQk3nRpUbd%2BhU6NWj7rlUk37Gl0tROCWvqOe2A%2F7kdpcs2w2nyD65YYezb6L8ybRfLFYwKWz2rb0iPQqdUMYgWJMgCsJZ6sktK%2FxCJ5AH0MLw7SjLmV6zb46SCH5DvpFxorL6UEJ5FpXaEZYf8hDAdex6a3zV1TGxp8jksiU4sL0%2FDu%2Bju6vrLcj%2BQKsGw8DVaJe58wX45RUA%2FVNK71y99hQfyUD8SbTDhAEhrIZi7wjpgXfL69nRWLLOqpqt98ue62jkGkJsSA0XUrbK5JTZXSlZn79nzeecLt0K79nuZH1YxXBTzC2VLsmx5UYKLwPTO8As4qzGf8wh2NSMnZrq2IQeFufedScm6DgsveuTX2QgHU5SwjH0gWeZCDw
![](https://prod-files-secure.s3.us-west-2.amazonaws.com/871f1661-80b8-4d0c-ac3b-2adfc6ff4c66/1022d66b-64fe-4c51-80a6-201e41228f5b/image?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=ASIAZI2LB46624BQZZUU%2F20260214%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20260214T173820Z&X-Amz-Expires=3600&X-Amz-Security-Token=IQoJb3JpZ2luX2VjEEEaCXVzLXdlc3QtMiJGMEQCIHlyC0SZ88a05iCRk2UUnGk3bqFwDokQfucQezc9dwKTAiAHdmZvdGWAY8MLAsM1yq7oYnO0UcWKY1jXUt0VbpN0zyr%2FAwgKEAAaDDYzNzQyMzE4MzgwNSIMxJS%2FCjNQEXBdPcSeKtwDiexq0qTZYEYTaYhww%2BFq5JwyxJxRGtVphJnDY2WI7IIx9%2BYC6pYsuPob4zguzeQk3nRpUbd%2BhU6NWj7rlUk37Gl0tROCWvqOe2A%2F7kdpcs2w2nyD65YYezb6L8ybRfLFYwKWz2rb0iPQqdUMYgWJMgCsJZ6sktK%2FxCJ5AH0MLw7SjLmV6zb46SCH5DvpFxorL6UEJ5FpXaEZYf8hDAdex6a3zV1TGxp8jksiU4sL0%2FDu%2Bju6vrLcj%2BQKsGw8DVaJe58wX45RUA%2FVNK71y99hQfyUD8SbTDhAEhrIZi7wjpgXfL69nRWLLOqpqt98ue62jkGkJsSA0XUrbK5JTZXSlZn79nzeecLt0K79nuZH1YxXBTzC2VLsmx5UYKLwPTO8As4qzGf8wh2NSMnZrq2IQeFufedScm6DgsveuTX2QgHU5SwjH0gWeZCDwqbm00Yw8w8mVTAjj4JVLFNuv3%2FmK09j8y1jRvGu0DTRUoIME%2FwTMKR8o81GpaqJWOWbmIK2qXo5RxtCg9K%2FJ2SD0mAdgpdWb8QFO%2B4%2Bt93mClZdP8MTFO%2BOmitASQ1ZI%2BBrJ5yolIzvfEKD3Vec4SyC%2F8u6y0lyReTwBZwMdjitO8wNVXzqOZ%2FQ6Un2T4HLpWYw39HCzAY6pgG%2BW3LNmFAz8K6wCm8%2BIzqSjKiVredRwkG2AOQcY8sBb4dNptXGaaZJbbnjHxmu%2BATOwYEpCJ4v2uxBtJS46UM5Cbx0Pi9N4z5rR68ljyLKJHt5FcSfUFmBaRCJkYgxTVpt95vAjjMWC9c%2FRhEga6T12yqYNn0qgaGLoiZ%2BAvwE5eGgiwk%2F69UwPqDTPOHqFlt8IDIYwYwti81Ie7Ea7B7n97arpiQM&X-Amz-Signature=ec28dc7ed5877216c022e37d9cceee59c7baabff3f4ab4a182440a45393af0f2&X-Amz-SignedHeaders=host&x-amz-checksum-mode=ENABLED&x-id=GetObject) Python is the perfect tool to automate file organization. In this article you will learn how to use Python to rename, move, copy, and delete files and folders. I also included an example script that cleans up your Desktop. If you prefer a video, you can watch the tutorial here: ## How to navigate and change the directory in Python You can use os.getcwd() to get the current working directory, and os.chdir() to change into another directory: ```plain text import os print(os.getcwd()) os.chdir("/Users/patrick/Desktop/video-files") print(os.getcwd()) # /Users/patrick/Desktop/video-files ``` ## How to rename files in Python You can use os.rename() or pathlib.Path.rename(). This example changes filenames from - 'dictionary - python-course-3.mov' to --> - '03-python-course-dictionary.mov' ```plain text import os for file in os.listdir(): # split into base name and extension name, ext = os.path.splitext(file) splitted = name.split("-") splitted = [s.strip() for s in splitted] new_name = f"{splitted[3].zfill(2)}-{splitted[1]}-{splitted[2]}-{splitted[0]}{ext}" os.rename(file, new_name) ``` Or as modern alternative you can usepathlib.Path: ```plain text import os from pathlib import Path for file in os.listdir(): f = Path(file) # split into base name and extension name, ext = f.stem, f.suffix splitted = name.split("-") splitted = [s.strip() for s in splitted] new_name = f"{splitted[3].zfill(2)}-{splitted[1]}-{splitted[2]}-{splitted[0]}{ext}" f.rename(new_name) ``` ## How to create directories: You can use pathlib.Path.mkdir() or os.mkdir(): ```plain text from pathlib import Path Path("data").mkdir(exist_ok=True) # or import os if not os.path.exists("data"): os.mkdir("data") ``` ## How to move files in Python This can be done with the shutil module: ```plain text import shutil shutil.move('source', 'destination') # works for file and folder ``` ## How to copy files in Python This can be done with shutil.copy() or shutil.copy2(). The latter also copies all metadata: ```plain text import shutil shutil.copy('source', 'destination') # new metatags shutil.copy2('source', 'destination') # copies metadata, too ``` ## How to delete files and folders in Python For files we can use os.remove() and for empty folders os.rmdir(). To recursively delete non-empty folders we can use shutil.rmtree(): ```plain text os.remove("filename") # error if not found os.rmdir("folder") # error if not empty, or not found shutil.rmtree("folder") # works for non empty directories ``` ## Organize and clean up your Desktop with Python This example script can be used to move images, videos, screenshots, and audio files into corresponding folders. A great way to run this script automatically is by using a cron job. In this article you can learn how to schedule Python scripts as cron jobs with crontab (Mac/Linux). ```plain text import os import shutil audio = (".3ga", ".aac", ".ac3", ".aif", ".aiff", ".alac", ".amr", ".ape", ".au", ".dss", ".flac", ".flv", ".m4a", ".m4b", ".m4p", ".mp3", ".mpga", ".ogg", ".oga", ".mogg", ".opus", ".qcp", ".tta", ".voc", ".wav", ".wma", ".wv") video = (".webm", ".MTS", ".M2TS", ".TS", ".mov", ".mp4", ".m4p", ".m4v", ".mxf") img = (".jpg", ".jpeg", ".jfif", ".pjpeg", ".pjp", ".png", ".gif", ".webp", ".svg", ".apng", ".avif") def is_audio(file): return os.path.splitext(file)[1] in audio def is_video(file): return os.path.splitext(file)[1] in video def is_image(file): return os.path.splitext(file)[1] in img def is_screenshot(file): name, ext = os.path.splitext(file) return (ext in img) and "screenshot" in name.lower() os.chdir("/Users/patrick/Desktop") for file in os.listdir(): if is_audio(file): shutil.move(file, "Users/patrick/Documents/audio") elif is_video(file): shutil.move(file, "Users/patrick/Documents/video") elif is_image(file): if is_screenshot(file): shutil.move(file, "Users/patrick/Documents/screenshots") else: shutil.move(file, "Users/patrick/Documents/images") else: shutil.move(file, "Users/patrick/Documents") ```

Visibility

Visible to everyone

Reading Status

Related Bookmarks

My Note


Saved!

Annotations

Export as Markdown
+ Annotate selection

Add Annotation