-->
What is the OS module in Python, and what are its uses?

What is the OS module in Python, and what are its uses?

Back to top

Updated by Ashirafu Kibalama on September 17, 2024

"What is the purpose of the OS module in Python, and how is it utilized?"






What is the OS module in Python?

The OS module in Python is a standard library module that provides a way to use operating system-dependent functionality.


It allows you to interface with the underlying operating system in a platform-independent manner.

This includes file and directory operations, environment variable manipulation, and process management.


6 Uses of os Module in Python

The os module in Python uses operating system-dependent functionality, for example, reading or writing to the file system, handling directories, and accessing environment variables. Here are some common uses of the os module:


1) Working with Directories:

The OS Module in Python is used when working with Directories to get the current working Directory, change a directory, and list its contents.


Get Current Working Directory:



os.getcwd() # Getting the Current Working Directory



Example:


cwd = os.getcwd() # Getting the Current Working Directory

print(cwd)


OutPut:






Change Directory:



os.chdir('/path/to/new_directory') # Changing the Current Working Directory



List Directory Contents:




os.listdir('/path/to/new_directory') # List Directory Contents


Example:


ldc = os.listdir(r'C:\Users\User\Desktop\variables_python') # List Directory Contents
print(ldc)


OutPut:





2) File and Directory Operations:

The OS Module in Python is used in file and directory operations to create directories, create intermediate directories, remove directories, remove paths, and rename files or directories.


Create Directory:



os.mkdir(path) # Create Directory



Example:


os.mkdir(r'C:\Users\User\Desktop\variables_python\blog_post') # Create Directory



OutPut:




Create Intermediate Directories:




os.makedirs(path) # Create Intermediate Directories



Example:



os.makedirs(r'C:\Users\User\Desktop\variables_python\make_inter\blog') # Create Intermediate Directories



OutPut:



Remove Directory:



os.rmdir(path) # Remove Directory



Remove File:



os.remove(path) # Remove File



3) Environment Variables:

The OS Module in Python is used to get and set environment variables.


Get Environment Variable:


os.getenv('VARIABLE') # Get Environment Variable


Set Environment Variable:



os.environ['key'] # set Environment Variable



4) Path Operations:

The OS Module in Python is used to join paths, check if Path exists, if Path is a file, if Path is a directory, get absolute Path and split Path into head and tail.


Join Paths:


os.path.join(path1, path2) # join paths


Check if Path Exists:


os.path.exists(path) # Check if Path Exists


Check if Path is a File:


os.path.isfile(path) # Check if Path is a File


Check if Path is a Directory:


os.path.isdir(path) # Check if Path is a Directory


Get Absolute Path:


os.path.abspath(path) # Get Absolute Path


Split Path into Head and Tail:



os.path.split(path) # Split Path into Head and Tail



5) Process Management:

The OS Module in Python is used for process management. It gets process IDs, gets parent process IDs, and executes a system command.


Get Process ID:


os.getpid() # Get Process ID


Get Parent Process ID:


os.getppid() # Get Parent Process ID


Execute a System Command:


os.system(command) # Execute a System Command


Example:


os.system('echo "Hello World!"') # Execute a System Command


OutPut:




6) OS Information:

The OS Module in Python is used for getting OS information, such as the Name of the OS module imported.


Getting OS Information:


os_name = os.name # Name of the OS module imported



Conclusion

The os module is very powerful and provides a wide range of functionalities to interact with the operating system, making it essential for many Python scripts and applications that need to perform OS-level tasks.



Related Posts:

1 How do I fix OSError in Python? and What causes OSError?


2 2 Methods To Find Your Virtual Environment or Virtualenv in Python (With a Step-by-step YouTube Video)


3 What is a virtual environment in Python, and What are the uses of the virtual environment in Python?


4 How To Fix: OSError: [WinError 145] The Directory is not Empty Python / Python Remove Folder With Content


Other Posts:

Best 4 Free Exchange Rate APIs With Python Examples (Step-by-Step with YouTube Video)


Build a CV or Resume Generator pdf Flask Python With Source Code (Step-by-Step with YouTube Video) 


3 Ckeditor Alternative: How To Upload An Image Ckeditor Flask Alternative/ Summernote Image Upload Flask Python


A Beginner's Complete Guide: Deploy Flask App with SQLite/ PostgreSQL Database on cPanel/ shared hosting 


5 How to fix: Python was not found Error: Run without arguments to install from the Microsoft Store or disable this shortcut from Settings Manage App Execution Aliases.


6 How to Change or Activate a Virtual Environment in Pycharm Windows/ macOS