Python Directory Management
Python Directory Management refers to creating, deleting, navigating, renaming, and listing directories programmatically. Python makes this easier using built-in modules like os, os.path, pathlib and shutil, all of which provide cross-platform support.
Why do we need Directory Management
- Automates common folder operations (create, delete, rename, navigate) and helps in organizing files into structured workflows.
- Works consistently across operating systems (Windows, Linux, macOS) using built-in modules.
- Enables dynamic control over the working directory and supports nested folder handling.
os and os.path module
os module provides functions to interact with the operating system, such as managing directories, files, processes and environment variables. It is cross-platform and adapts to the underlying OS (e.g., Windows, Linux, macOS).
Creating new directory
Sometimes your program needs to create folders on the fly to store files or organize data. Python makes this easy with os.mkdir() for a single folder and os.makedirs() for creating nested directories in one go.
- os.mkdir(path): Creates a single directory at the specified path (error if it already exists).
- os.makedirs(path): Creates a directory along with any missing parent directories.
Example: This code creates one simple directory and one nested directory.
import os
os.mkdir("my_directory") # single directory
os.makedirs("parent_directory/child_directory") # nested directories
Explanation:
- os.mkdir("my_directory"): makes a folder in the current location.
- os.makedirs(): creates both parent_directory and child_directory if they donât already exist.
Getting Current Working Directory (CWD)
When working with relative paths, itâs important to know the folder where your Python script is running. Python provides two functions for this:
- os.getcwd(): Returns the current directory as a normal string.
- os.getcwdb(): Returns the same path as a byte string.
Example: This code prints the current working directory in two formats.
import os
print("String format :", os.getcwd())
print("Byte string format :", os.getcwdb())
Output
String format : /home/guest/sandbox Byte string format : b'/home/guest/sandbox'
Explanation:
- os.getcwd(): returns directory as a human-readable string.
- os.getcwdb(): returns the same path but in raw byte format.
Renaming a directory
Sometimes you may need to change a folderâs name or move it into a different location. Python provides:
- os.rename(src, dst): Renames or moves a file/directory.
- os.renames(src, dst): Same as above, but also creates any missing intermediate directories in the path.
Example: This code shows both simple renaming and renaming with path changes.
import os
# Simple rename
os.rename("my_directory", "renamed_directory")
# Rename and create missing parent directories if needed
os.renames("old_dir", "new_parent/new_child/renamed_directory")
Explanation:
- os.rename() changes the name of a file or directory. If "my_directory" exists, it's renamed to "renamed_directory".
- os.renames() renames the directory and also creates any missing intermediate directories in the destination path.
Changing Current Working Directory (CWD)
You can change where your script is working from. This helps when you want to run file operations inside a specific folder. os.chdir(path): Changes the working directory to the given path.
Example: This code shows the directory before and after changing it.
import os
print("Current directory :", os.getcwd())
os.chdir('/home/nikhil/Desktop/') # Changing directory
print("Current directory :", os.getcwd())
Output:
Current directory : /home/nikhil/Desktop/gfg
Current directory : /home/nikhil/Desktop
Explanation:
- os.getcwd() confirms the working directory before and after the change
- os.chdir() changes the current working directory to /home/nikhil/Desktop/.
Listing Files in a Directory
You can get the names of all files and folders inside a directory.
- os.listdir(path): Lists all files and subdirectories in the given path (non-recursive).
- For recursive listing, use os.walk().
Example: This code lists everything in the current directory.
import os
print("Files in CWD are :", os.listdir(os.getcwd()))
Output
Files in CWD are : ['output.txt', 'input.txt', 'driver', 'Solution.py']
Explanation: os.listdir() lists all files and folders in the current working directory (os.getcwd()).
Removing a directory
Directories can be removed, but methods differ depending on whether they are empty or contain files.
- os.rmdir(path): Removes an empty directory.
- shutil.rmtree(path): Removes a directory and all its contents (use with caution).
Example: This code checks if a directory is empty before deleting it.
import os
dir_path = "k:/files"
if not os.listdir(dir_path):
os.rmdir(dir_path)
print("Directory removed successfully")
else:
print("Error! Directory not empty.")
Explanation:
- Checks if the directory 'k:/files' is empty using os.listdir(dir_path).
- If it is empty, removes it using os.rmdir(); otherwise, prints an error message.
Check Whether It Is a Directory
Before performing operations, itâs good practice to confirm if a given path is actually a directory. os.path.isdir(path) Returns True if the path is a directory, else False.
Example: This code checks two paths to see if they are directories.
import os
cwd = '/'
print(os.path.isdir(cwd)) # True if cwd is a directory
other = 'K:/'
print(os.path.isdir(other)) # Checks another directory
Output
True False
Working with Directories using pathlib
pathlib is a modern, object-oriented alternative to os and os.path. It makes code cleaner and easier to read.
- Path().mkdir(): Creates a directory.
- Path().iterdir(): Lists files/folders in a directory.
Example: This code creates a folder and lists files in the current directory.
from pathlib import Path
Path("my_new_dir").mkdir(exist_ok=True)
for file in Path(".").iterdir():
print(file)
Explanation:
- Path("my_new_dir").mkdir(exist_ok=True) creates the folder if it doesnât already exist.
- Path(".").iterdir() lists all files and directories in the current working directory.
Get Size of the Directory
To calculate the size of a directory, you need to add up the sizes of all its files (recursively).
- os.path.getsize(path): Returns size of a file in bytes.
- os.walk(path): Iterates through all subfolders and files.
Example: This code computes the total size of the current directory.
import os
total_size = 0
for dirpath, dirnames, filenames in os.walk(os.getcwd()):
for f in filenames:
fp = os.path.join(dirpath, f)
total_size += os.path.getsize(fp)
print("Total directory size:", total_size, "bytes")
Output
Total directory size: 23541 bytes
Explanation: os.walk() iterates through all files in the directory tree and os.path.getsize() adds up their sizes, giving the total size of contents.
Getting Access and Modification Times
Letâs explore how to retrieve the last access and modification times of files or directories in Python. This is useful for tracking file usage, backups or system monitoring tasks.
- os.path.getatime(path): Returns the last access time of a file or directory as a timestamp.
- os.path.getmtime(path): Returns the last modification time as a timestamp.
Example : Getting access and modification time of GeeksforGeeks (root) directory
import time
import os
# Get times
access_time = os.path.getatime("/")
modification_time = os.path.getmtime("/")
# Convert to readable format
print("Access Time:", time.ctime(access_time))
print("Modification Time:", time.ctime(modification_time))
Output
Access Time: Sat Jan 4 09:21:37 2025 Modification Time: Sat Jan 4 09:21:37 2025
Explanation: time.ctime() converts the timestamp into human-readable format.
shutil module
shutil module in Python is a high-level file and directory management library. It provides functions for copying, moving and removing files and directories.
shutil.copytree()
Recursively copies an entire directory tree (source directory and all its contents) to a destination. Creates a new directory at the destination path and copies all files and subdirectories. Raises FileExistsError if the destination exists and dirs_exist_ok is False.
Syntax:
shutil.copytree(src, dst, dirs_exist_ok=False)
Parameters:
- src: Path to the source directory.
- dst: Path to the destination directory.
- dirs_exist_ok: If True, allows copying into an existing directory. If False (default), raises an error if the destination already exists.
Example: This code copies all files and folders from source_dir into destination_dir.
import shutil
shutil.copytree("source_dir", "destination_dir", dirs_exist_ok=True)
print("Directory copied successfully")
Explanation: Recursively copies the contents of source_dir (including files and subdirectories) into a new destination_dir.
shutil.rmtree()
Deletes an entire directory tree, including all its files and subdirectories. This operation is irreversible. Be careful when specifying the path.
Syntax:
shutil.rmtree(path, ignore_errors=False)
Parameters:
- path: Path to the directory to be removed.
- ignore_errors: If True, ignores errors during removal. If False (default), raises an error.
Example: This code permanently deletes the destination_dir folder and everything inside it.
import shutil
shutil.rmtree("destination_dir")
print("Directory removed successfully")
Explanation: Recursively deletes destination_dir and all its contents.
Tip: If you want safer deletion (sending files to recycle bin instead of permanent removal), use send2trash module instead of shutil.rmtree().
shutil.move(s,d)
Moves a file or directory to a new location. If the source and destination are on the same filesystem, this is equivalent to renaming. Otherwise, it copies the source to the destination and then deletes the original.
Syntax:
shutil.move(src, dst)
Parameters:
- src: Path to the source file or directory.
- dst: Path to the destination file or directory.
Example: This code moves the entire source_dir folder into new_location.
import shutil
shutil.move("source_dir", "new_location")
print("Directory moved successfully")
Explanation: Moves the entire source_dir to new_location. If both are on the same filesystem, it's a rename operation otherwise, it copies and deletes the source.