Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is Python's OS Module
The OS module in Python provides functions that enable developers to interact with the operating system. This built-in module allows you to perform common file and directory operations like creating folders, deleting files, and navigating directories.
Importing the OS Module
Python's OS module comes pre-installed with Python, so no separate installation is required. Simply import it to access its functions ?
import os
Getting Current Working Directory
The current working directory is the folder where your Python script is located and executed from ?
import os
current_dir = os.getcwd()
print("Current working directory:", current_dir)
Current working directory: /home/user/projects
Note: Directory is another term for folder.
Creating a Directory
Use mkdir() to create a new directory ?
import os
# Create a directory in current location
os.mkdir("tutorial_folder")
print("Directory 'tutorial_folder' created successfully")
Directory 'tutorial_folder' created successfully
You can also specify a full path. If no location is specified, the directory will be created in the current working directory.
Deleting a Directory
Use rmdir() (remove directory) to delete an empty directory ?
import os
# Delete the directory we created
os.rmdir("tutorial_folder")
print("Directory deleted successfully")
Note: You cannot delete the current working directory (the folder containing your running Python script), and the directory must be empty.
Renaming a Directory
Use rename() to rename directories or files ?
import os
# Create and then rename a directory
os.mkdir("old_name")
os.rename("old_name", "new_name")
print("Directory renamed from 'old_name' to 'new_name'")
Basic File Operations
The OS module also provides basic file manipulation capabilities.
Creating and Writing to a File
import os
# Create and write to a file
file = os.popen("hello.txt", 'w')
file.write("Hello there! This is a TutorialsPoint article")
file.close()
print("File created and content written")
Note: You can also use os.rename() to rename files. Make sure to include the correct file extensions.
Complete Example
Here's a comprehensive example demonstrating various OS module functions ?
import os
# Get current working directory
print("Current directory:", os.getcwd())
# Create a directory
os.mkdir("demo_folder")
print("Created 'demo_folder'")
# Rename the directory
os.rename("demo_folder", "renamed_folder")
print("Renamed to 'renamed_folder'")
# Create a file
file = os.popen("demo.txt", 'w')
file.write("This is a demo file created using OS module")
file.close()
print("Created and wrote to 'demo.txt'")
# Clean up - remove directory (after removing file if needed)
os.remove("demo.txt") # Remove file first
os.rmdir("renamed_folder") # Then remove directory
print("Cleanup completed")
Conclusion
The OS module provides essential functions for interacting with the operating system, including directory and file operations. These basic functions form the foundation for more complex file system operations in Python development.
