Python OS Module
The os module in Python provides functions for interacting with the operating system. This module helps you with tasks such as file and directory manipulation, environment variables, and process management.
Common Functions in the os Module
- os.getcwd(): Returns the current working directory.
- os.listdir(path): Returns a list of the entries in the directory given by path.
- os.mkdir(path): Creates a directory named path.
- os.remove(path): Deletes the file path.
- os.rename(src, dst): Renames the file or directory from src to dst.
- os.environ: A dictionary representing the environment variables.
Examples
1. Current Working Directory
import os
# Example: Getting the current working directory
print(os.getcwd())
Output
/path/to/current/directory
2. Directory Listing
import os
# Example: Listing files in a directory
print(os.listdir('.'))
Output
['file1.txt', 'file2.py', 'folder1']
The os module in Python allows easy interaction with the operating system, making it a powerful tool for file management, environment handling, and system-related tasks.