Python OS Module tutorial shows how to use the OS module for interacting with the operating system in Python.
last modified February 15, 2025
In this article, we show how to use the os module in Python. The os module provides a way of interacting with the operating system, allowing you to perform tasks such as file and directory manipulation, environment variable management, and process management.
The os module is particularly useful for tasks like navigating the file system, creating and deleting files, and working with environment variables.
The following example demonstrates how to use the os module to navigate the file system.
main.py
import os
current_directory = os.getcwd() print(“Current Directory:”, current_directory)
os.chdir(’/tmp’) print(“Changed Directory:”, os.getcwd())
print(“Files and Directories:”, os.listdir())
In this program, the os.getcwd function is used to get the current working directory, os.chdir is used to change the directory, and os.listdir is used to list files and directories.
The following example demonstrates how to create, rename, and delete files and directories using the os module.
main.py
import os
os.mkdir(’new_directory’) print(“Directory Created:”, ’new_directory')
os.rename(’new_directory’, ‘renamed_directory’) print(“Directory Renamed:”, ‘renamed_directory’)
with open(’new_file.txt’, ‘w’) as f: f.write(‘Hello, World!’) print(“File Created:”, ’new_file.txt’)
os.remove(’new_file.txt’) print(“File Deleted:”, ’new_file.txt’)
os.rmdir(‘renamed_directory’) print(“Directory Deleted:”, ‘renamed_directory’)
In this program, the os.mkdir function is used to create a directory, os.rename is used to rename it, os.remove is used to delete a file, and os.rmdir is used to delete a directory.
$ python main.py Directory Created: new_directory Directory Renamed: renamed_directory File Created: new_file.txt File Deleted: new_file.txt Directory Deleted: renamed_directory
The following example demonstrates how to work with environment variables using the os module.
main.py
import os
home_directory = os.getenv(‘HOME’) print(“Home Directory:”, home_directory)
os.environ[‘MY_VAR’] = ‘my_value’ print(“MY_VAR:”, os.getenv(‘MY_VAR’))
print(“Environment Variables:”, os.environ)
In this program, the os.getenv function is used to retrieve the value of an environment variable, os.environ is used to set a new environment variable, and os.environ is also used to list all environment variables.
The following example demonstrates how to run system commands using the os module.
main.py
import os
os.system(’ls -l’)
In this program, the os.system function is used to execute the ls -l command, which lists files and directories in the current directory.
The following example demonstrates how to manipulate file paths using the os.path submodule.
main.py
import os
path = os.path.join(’/home’, ‘user’, ‘documents’, ‘file.txt’) print(“Joined Path:”, path)
basename = os.path.basename(path) print(“Basename:”, basename)
dirname = os.path.dirname(path) print(“Dirname:”, dirname)
exists = os.path.exists(path) print(“Path Exists:”, exists)
In this program, the os.path.join function is used to join path components, os.path.basename is used to get the basename of the path, os.path.dirname is used to get the directory name, and os.path.exists is used to check if the path exists.
The following example demonstrates how to rename a file using the os.rename function and check if the file exists using os.path.exists.
main.py
import os
file_name = ‘myfile.txt’
if os.path.exists(file_name): # Rename the file os.rename(‘myfile.txt’, ‘myfile2.txt’) print(“File renamed successfully.”) else: print(‘Failed to rename file: File does not exist.’)
In this program, the os.path.exists function is used to check if the file exists, and os.rename is used to rename the file if it exists. If the file does not exist, an appropriate message is displayed.
$ python main.py File renamed successfully.
If the file does not exist, the program will output:
$ python main.py Failed to rename file: File does not exist.
The following example demonstrates how to list the contents of a directory using the os.listdir function.
main.py
import os
content = os.listdir(’.’) print(“Directory Contents:”, content)
In this program, the os.listdir function is used to list all entries in the current directory. The list includes files and directories but excludes special entries like ‘.’ and ‘..’.
The following example demonstrates how to list the contents of a directory using the os.scandir function, which provides additional information such as file attributes.
main.py
import os from datetime import datetime
path = ‘.’
with os.scandir(path) as it: for entry in it: print(f’{entry.name} - Created: {datetime.fromtimestamp(entry.stat().st_ctime)}’)
In this program, the os.scandir function is used to list entries in the current directory along with their creation time. The entry.stat method provides file metadata, such as creation time (st_ctime).
The following example demonstrates how to traverse a directory recursively using the os.walk function. This function generates the file names and directory names in a directory tree by walking either top-down or bottom-up.
main.py
import os
for root, dirs, files in os.walk(os.path.abspath(".")): # Print all files in the current directory for name in files: print(os.path.join(root, name)) # Print all subdirectories in the current directory for name in dirs: print(os.path.join(root, name))
In this program, the os.walk function is used to traverse the directory tree starting from the current directory. For each directory, it lists all files and subdirectories, printing their full paths using os.path.join.
The os.walk function is particularly useful for recursively processing files and directories in a directory tree.
Python OS Module - Documentation
In this article, we have shown how to use the os module for interacting with the operating system in Python.
My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.
List all Python tutorials.