Mastering Directory and File Operations in Python
Python Directory and Files Management
Discover how to efficiently create, navigate, list, rename, and delete directories and files in Python with clear, hands‑on examples.
Video: Python os Module
Python Directory
When your project grows, organizing code into well‑structured folders becomes essential. A directory—or folder—is a container for files and sub‑directories, and Python’s os module supplies robust tools to manage them.
Get Current Directory
Use os.getcwd() to retrieve the current working directory as a string. If you need a bytes representation, os.getcwdb() will do the trick.
import os
os.getcwd()
# 'C:\\Program Files\\PyScripter'
os.getcwdb()
# b'C:\\Program Files\\PyScripter'
The double backslash indicates an escape sequence. Using print() renders the path correctly:
print(os.getcwd())
# C:\Program Files\PyScripter
Changing Directory
Switch the current working directory with os.chdir(path). The path argument can use either forward or backslashes; just remember to escape backslashes or use raw strings.
os.chdir('C:\\Python33')
print(os.getcwd())
# C:\Python33
List Directories and Files
Retrieve every file and sub‑directory in a target folder with os.listdir(path). If path is omitted, the function operates on the current working directory.
print(os.getcwd())
# C:\Python33
os.listdir()
# ['DLLs', 'Doc', 'include', 'Lib', 'libs', 'LICENSE.txt', 'NEWS.txt', 'python.exe', 'pythonw.exe', 'README.txt', 'Scripts', 'tcl', 'Tools']
os.listdir('G:\\')
# ['$RECYCLE.BIN', 'Movies', 'Music', 'Photos', 'Series', 'System Volume Information']
Making a New Directory
Create a folder with os.mkdir(path). If the path is relative, the new directory appears in the current working directory.
os.mkdir('test')
print(os.listdir())
# ['test']
Renaming a Directory or a File
The same os.rename(src, dst) function handles both files and folders. Provide the old name as src and the desired new name as dst:
os.listdir()
# ['test']
os.rename('test', 'new_one')
print(os.listdir())
# ['new_one']
Removing Directory or File
Delete a file with os.remove(path). To remove an empty folder, use os.rmdir(path):
os.listdir()
# ['new_one', 'old.txt']
os.remove('old.txt')
print(os.listdir())
# ['new_one']
os.rmdir('new_one')
print(os.listdir())
# []
Note: os.rmdir() only works on empty directories.
If you need to delete a folder that contains files or sub‑folders, the shutil.rmtree() function from the shutil module is the way to go:
os.listdir()
# ['test']
os.rmdir('test')
# Traceback (most recent call last):
# ...
# OSError: [WinError 145] The directory is not empty: 'test'
import shutil
shutil.rmtree('test')
print(os.listdir())
# []
Python
- Python Keywords and Identifiers: Mastering Reserved Words and Naming Conventions
- Python Statements, Indentation, and Comments: A Clear Guide
- Python Variables, Constants, and Literals – A Comprehensive Guide
- Mastering Python Type Conversion & Casting: A Comprehensive Guide
- Mastering Python I/O and Module Imports: A Practical Guide
- Python Namespaces & Variable Scope: Understanding Names, Bindings, and Scopes
- Mastering Python Loop Control: break & continue
- How to Rename Files and Directories in Python with os.rename() – Step-by-Step Guide
- Python vs JavaScript: Key Differences, Features, and When to Choose Each
- Python vs Ruby: A Comprehensive Comparison of Features, Advantages, and Use Cases