Master File and Directory Manipulation with Python's os Module!
To run Python from the command prompt or PowerShell on your PC, you need to download and install Python.
If you haven’t installed it yet, please refer to the article Setting Up Python and Development Environment to install Python.
When you're running a website, you often find yourself needing to manipulate files on the server, like organizing uploaded images or taking regular backups, right? That's when Python's os module comes in handy.
The os module is a standard library in Python for interacting with the operating system (OS), especially for file system operations. With it, you can automate tasks you usually do with a mouse or command line, like creating, deleting, and renaming files and directories, all from within a program.
In this article, we'll introduce plenty of copy-paste-ready code, from the basic usage of the os module to practical application examples, so that web creators like you can immediately use it when you think, "I want to automate a small task." Let's put the complicated theory aside for now and first experience the joy of making things work! 🚀
Basic Usage of the os Module
First, the fundamentals. Let's look at the setup for using the os module and its most commonly used basic functions. Any complex operation is just a combination of these basics.
1. Importing the os Module
To use the os module, you first need to import it at the beginning of your Python script. Think of it as a magic spell.
import os
2. Check the Current Working Directory - getcwd()
Knowing where your program is currently running (the current working directory) is fundamental to any operation. You can check it using `os.getcwd()`. (getcwd is short for get current working directory).
import os
# Get and display the current working directory
current_path = os.getcwd()
print(f"Current directory: {current_path}")
# Example output:
# Current directory: /Users/yourname/Documents
3. Create a Directory - mkdir(), makedirs()
Let's create a new folder (directory). `os.mkdir()` creates a single directory. If you want to create a deep hierarchy of directories all at once, `os.makedirs()` is convenient.
First, here's an example of creating just one with `mkdir()`.
import os
# Create a directory named "my_project"
# Make sure a directory with the same name does not exist before running
if not os.path.exists("my_project"):
os.mkdir("my_project")
print("Created 'my_project' directory.")
else:
print("'my_project' directory already exists.")
Next, let's use `makedirs()` to create parent-child directories like `parent/child` all at once. Adding `exist_ok=True` is a safe bet, as it prevents errors even if the directory already exists.
import os
# Create a directory hierarchy "contents/images" at once
# With exist_ok=True, it won't error out if the directory already exists
os.makedirs("contents/images", exist_ok=True)
print("Created 'contents/images' directory (or it already existed).")
4. List Files and Directories - listdir()
If you want to know what files and folders are inside a specific directory, use `os.listdir()`. If you don't specify a path as an argument, it returns a list of items in the current working directory.
import os
# Get a list of files and directories in the current directory
# If you've created "my_project" or "contents" beforehand, they will be listed
items = os.listdir('.') # '.' refers to the current directory
print(f"Contents of the current directory: {items}")
# Example output:
# Contents of the current directory: ['my_project', 'contents', 'my_script.py']
5. Safely Join Paths - path.join()
Manually concatenating path strings like `"folder" + "/" + "file.txt"` is risky! This is because the path separator differs between operating systems (Windows uses `\`, while Mac and Linux use `/`). `os.path.join()` is very safe because it joins paths with the appropriate separator for the OS it's running on.
import os
# OS-independent safe path joining
dir_name = "assets"
file_name = "style.css"
# This code works correctly on Windows, Mac, and Linux
correct_path = os.path.join(dir_name, "css", file_name)
print(f"Generated path: {correct_path}")
# Example output (Mac/Linux):
# Generated path: assets/css/style.css
# Example output (Windows):
# Generated path: assets\css\style.css
6. Rename / Move a File or Directory - rename()
`os.rename()` can be used not only to change the name of a file or directory but also to move it. If you specify a different directory as the destination, you can move it there.
import os
# Create a dummy file named "old_name.txt"
with open("old_name.txt", "w") as f:
f.write("test")
# Rename "old_name.txt" to "new_name.txt"
if os.path.exists("old_name.txt"):
os.rename("old_name.txt", "new_name.txt")
print("Renamed old_name.txt to new_name.txt.")
# Confirmation
print(f"Contents of the current directory: {os.listdir('.')}")
7. Delete a File or Directory - remove(), rmdir()
Deleting files and directories is also easy. Use `os.remove()` to delete a file and `os.rmdir()` to delete an empty directory. Be careful, as `rmdir()` cannot delete a directory that contains files.
import os
# Create a dummy file named "temp_file.txt"
with open("temp_file.txt", "w") as f:
f.write("delete me")
# Create a dummy directory named "temp_dir"
if not os.path.exists("temp_dir"):
os.mkdir("temp_dir")
# Delete the file
if os.path.exists("temp_file.txt"):
os.remove("temp_file.txt")
print("Deleted temp_file.txt.")
# Delete the empty directory
if os.path.exists("temp_dir"):
os.rmdir("temp_dir")
print("Deleted temp_dir.")
【Hands-on】Let's Use the os Module!
Now that you understand the basics, let's look at some more practical code. These examples might just make your daily tasks a little easier.
Application Example 1: Organize Multiple Image Files by Extension
Is your downloads folder or assets folder a jumble of image files like JPG, PNG, and SVG? Here is a script that automatically sorts them into folders based on their file extension.
import os
# --- Preparation: Create dummy files to be organized ---
# Create a working directory named "image_sorter"
base_dir = "image_sorter"
os.makedirs(base_dir, exist_ok=True)
# Create dummy image files
dummy_files = ["cat.jpg", "dog.png", "logo.svg", "photo.jpg", "icon.png"]
for fname in dummy_files:
# Safely generate path using os.path.join
with open(os.path.join(base_dir, fname), "w") as f:
f.write(f"This is {fname}.")
print(f"Contents of {base_dir} before sorting: {os.listdir(base_dir)}")
print("-" * 20)
# --- End of preparation ---
# --- Main process: Organize the files ---
# Specify the target directory
target_dir = "image_sorter"
# Get all files in the directory
for filename in os.listdir(target_dir):
# Generate the full path of the file
full_path = os.path.join(target_dir, filename)
# If it's not a file (= it's a directory), skip it
if not os.path.isfile(full_path):
continue
# Get the extension from the filename (e.g., ".jpg")
# os.path.splitext is a handy function that splits the filename and extension
ext = os.path.splitext(filename)[1] # Get the second element of ("cat", ".jpg")
# Skip files with no extension
if not ext:
continue
# Create a folder named after the extension without the "." (e.g., "jpg" folder)
# Convert extension to lowercase for consistency
folder_name = ext[1:].lower()
ext_dir = os.path.join(target_dir, folder_name)
# Create the folder if it doesn't exist
os.makedirs(ext_dir, exist_ok=True)
# Move the file to the new folder
new_path = os.path.join(ext_dir, filename)
os.rename(full_path, new_path)
print(f"Moved {filename} to the {folder_name} folder.")
print("-" * 20)
print(f"Contents of {base_dir} after sorting: {os.listdir(base_dir)}")
print(f"Contents of jpg folder: {os.listdir(os.path.join(base_dir, 'jpg'))}")
print(f"Contents of png folder: {os.listdir(os.path.join(base_dir, 'png'))}")
【Full Copy-Paste OK】Code to Automatically Generate a Test Environment on Your Desktop
After reading this far, some of you might be wondering, "I want to try this on my PC, but where should I run it?" So, we've prepared a "ready-to-run" Python code that you can just copy, save as a file like `create_test_env.py`, and execute to automatically generate a test folder and files on your PC's desktop. Check your desktop after running it!
(※This code creates a folder named `python_os_test` on your desktop. Please delete the folder when you no longer need it.)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Python os Module Code Example</title>
<style>
body { background-color: #202124; color: #e8eaed; font-family: sans-serif; }
pre { background-color: #282c34; color: #abb2bf; padding: 1em; border-radius: 5px; overflow-x: auto; }
code { font-family: 'Courier New', Courier, monospace; }
h1 { color: #669df6; }
p { line-height: 1.6; }
</style>
</head>
<body>
<h1>Python Code: Automatically Generate a Test Environment on Your Desktop</h1>
<p>Copy the Python code below, save it as `create_test_env.py`, and run `python create_test_env.py` from your terminal or command prompt.</p>
<pre><code>
import os
import sys
def create_test_environment():
"""
A function to generate test directories and files on the desktop.
"""
try:
# 1. Get the desktop path
# os.path.expanduser('~') gets the home directory
desktop_path = os.path.join(os.path.expanduser('~'), 'Desktop')
# For OS environments without a desktop (like a CUI server), use the current directory
if not os.path.exists(desktop_path):
desktop_path = os.getcwd()
print(f"Desktop not found. Creating in the current directory: {desktop_path}")
# 2. Create the main test folder
main_test_dir = os.path.join(desktop_path, "python_os_test")
os.makedirs(main_test_dir, exist_ok=True)
print(f"Created main folder: {main_test_dir}")
# 3. Create subfolders
sub_dirs = ["documents", "images", "archives"]
for sub_dir in sub_dirs:
os.makedirs(os.path.join(main_test_dir, sub_dir), exist_ok=True)
print(f"Created subfolders: {sub_dirs}")
# 4. Create dummy files
files_to_create = {
"documents": ["report.txt", "memo.md"],
"images": ["cat.jpg", "dog.png"],
"": ["readme.txt"] # Directly under the main folder
}
for directory, filenames in files_to_create.items():
for filename in filenames:
file_path = os.path.join(main_test_dir, directory, filename)
with open(file_path, "w", encoding="utf-8") as f:
f.write(f"This is test content for {filename}.")
print("Created dummy files.")
print("\n")
print("🎉 Setup complete!")
print(f"Please check the {main_test_dir} directory.")
except OSError as e:
print(f"An error occurred: {e}", file=sys.stderr)
print("This could be a permissions issue or a problem with the path.", file=sys.stderr)
except Exception as e:
print(f"An unexpected error occurred: {e}", file=sys.stderr)
if __name__ == "__main__":
create_test_environment()
</code></pre>
</body>
</html>
3 Key Points to Watch Out For
The os module is powerful, but because it directly manipulates the file system, there are a few points to be careful about. Just knowing these can prevent many errors.
- Incorrect Path Specification
Most "No such file or directory" errors (FileNotFoundError) are caused by incorrect path specifications. Get into the habit of checking your current location with `os.getcwd()` and pre-checking the existence of your target with `os.path.exists()`. - Don't Neglect Error Handling
Trying to create an existing directory with `os.mkdir()` or delete a non-existent file with `os.remove()` will cause your program to crash. Writing your code to handle potential errors using pre-checks with `os.path.exists()` or `try...except` blocks will result in a more robust program. - Irreversible Operations
Operations like `os.remove()` and `os.rename()` are generally not "undoable." They don't use the PC's recycle bin feature. Especially when dealing with important files, it's safer to add a buffer step, like moving them to a backup folder first, instead of deleting them outright.
Even More Convenient! Introduction to Related Modules
The os module is fundamental, but more modern and convenient libraries also exist. As a next step, it's a good idea to get to know these libraries as well.
pathlib - Modern and Intuitive Path Manipulation
`pathlib`, added to the standard library in Python 3.4, is a module that allows you to treat paths as objects in an intuitive way. Since they are objects rather than strings, you can chain methods, join paths with the `/` operator, and generally write cleaner code.
from pathlib import Path
# Get the current directory as a Path object
p = Path('.')
# Directory creation using pathlib
new_dir = p / "pathlib_test" # You can join paths with the / operator!
new_dir.mkdir(exist_ok=True) # Execute as a method
# Listing files is also easy
for item in p.iterdir():
print(f"{'Directory' if item.is_dir() else 'File'}: {item.name}")
shutil - High-Level File Operations
`shutil` (shell utilities) provides higher-level file operations than the os module. For example, copying or deleting a directory with its contents, which would require you to write a recursive function yourself with the os module, can be done in a single line with `shutil`.
import shutil
import os
# --- Preparation ---
os.makedirs("source_dir/sub_dir", exist_ok=True)
with open("source_dir/sub_dir/data.txt", "w") as f:
f.write("important data")
# --- shutil usage example ---
# Copy an entire directory
if os.path.exists("source_dir"):
shutil.copytree("source_dir", "backup_dir")
print("Copied source_dir to backup_dir.")
# Delete a directory and its contents
if os.path.exists("backup_dir"):
shutil.rmtree("backup_dir")
print("Deleted backup_dir and its contents.")
# Also delete the now-unneeded source_dir
shutil.rmtree("source_dir")
Conclusion
In this article, we've covered everything from the basics to application examples of file and directory manipulation using Python's os module. Behind the scenes of web development, many such tedious file operations occur. By mastering the os module, you should be able to automate these annoying manual tasks and focus more on your creative work.
First, try copying and pasting the code from this article to get a feel for "making files move with a program!" From there, try to customize it to fit your own work needs!
To the Next Step
After file manipulation, why not explore the world of numerical computation? With Python's math module, you can easily perform various mathematical calculations, such as trigonometric functions and logarithms.
➡️ Master Mathematical Functions with the math Module