Python OS Module
The Python os module is an important part of the Python standard library that allows programs to interact with the operating system. It provides functions for working with files and directories, changing locations, checking paths, renaming files, creating folders, and accessing system-related information.
Using the os module, developers can perform many operating-system-related tasks directly from Python code. This makes it useful for scripts and applications that need to manage files, directories, paths, and other system resources.
In this article, we will explore the Python os module and several commonly used functions, including os.name, os.mkdir(), os.getcwd(), os.chdir(), os.rmdir(), os.popen(), os.close(), os.rename(), and os.access().
Table of Contents

How to Use the Python OS Module
The os module is included with Python, so you can import it directly into your program. No separate installation is normally required.
Use the following statement to import it:
import os
After importing the module, you can access its functions and attributes using the os namespace.
Download New Real Time Projects: Click here
Key Functions of the Python OS Module
The os module contains many functions for working with the operating system. The following sections explain some commonly used features with examples.
1. os.name
The os.name attribute identifies the operating-system-dependent module that Python is using. It can help determine the general operating-system family on which a Python program is running.
Example
import os
print(os.name)
Output:
nt
For example, nt is commonly associated with Windows, while posix is commonly associated with Unix-like operating systems.
2. os.mkdir()
The os.mkdir() function is used to create a new directory. You provide the directory path or name that you want Python to create.
Example
import os
os.mkdir(r"d:\newdir")
This example creates a directory named newdir on the D: drive, provided that the specified location is available and the program has the required permissions.
The raw-string notation r is used here to make Windows paths easier to write without treating backslashes as escape characters.
3. os.getcwd()
The os.getcwd() function returns the current working directory of the Python process. The current working directory is the location from which relative file and directory operations are generally performed.
Example
import os
print(os.getcwd())
Example Output:
C:\Users\Python\Desktop\ModuleOS
The exact output depends on the directory from which the Python program is running.
4. os.chdir()
The os.chdir() function changes the current working directory of the Python process. This can be useful when a program needs to perform operations inside another directory.
Example
import os
os.chdir(r"d:\")
print(os.getcwd())
Example Output:
D:\
After calling os.chdir(), subsequent operations that depend on the current working directory will use the new location.
5. os.rmdir()
The os.rmdir() function removes a directory. The directory must be empty before it can normally be removed using this function.
Example
import os
os.rmdir(r"d:\newdir")
In this example, the newdir directory is removed from the specified location. If the directory contains files or other directories, the operation will not remove it as a non-empty directory.
6. Handling OS-Related File Errors
When Python works with files and directories, operations can fail for several reasons. A file may not exist, a path may be incorrect, or the program may not have sufficient permissions.
These situations can be handled with Python’s exception-handling mechanism. For example:
import os
try:
filename = "Python.txt"
f = open(filename, "r")
text = f.read()
f.close()
except IOError:
print("Problem reading: " + filename)
Output:
Problem reading: Python.txt
If the file cannot be opened or read, the exception handler displays the specified message. This approach helps prevent the program from stopping unexpectedly when a file operation fails.
7. os.popen()
The os.popen() function can open a pipe to or from a command and return a file-like object. This allows a Python program to communicate with the command through the returned object.
Example
import os
file = os.popen("python.txt", "w")
file.write("This is awesome")
file.close()
The popen() function is related to executing commands and working with their input or output. The exact behavior depends on the command supplied and the operating environment.
When working with external commands in modern Python applications, developers should carefully consider the command being executed and the input supplied to it.
8. os.close()
The os.close() function is used to close an operating-system-level file descriptor. It is important to distinguish a file descriptor from a normal Python file object.
For example, os.open() returns a file descriptor that can be closed with os.close().
import os
fd = os.open("Python1.txt", os.O_RDONLY)
os.close(fd)
The os.close() function should not be used directly on a normal Python file object returned by the built-in open() function. Normal Python file objects should be closed using their own close() method or managed with a with statement.
9. os.rename()
The os.rename() function can be used to rename a file or directory. It accepts the existing path and the new name or path.
Example
import os
fd = "python.txt"
os.rename(fd, "Python1.txt")
After successful execution, the file named python.txt is renamed to Python1.txt. The program must have the necessary permissions, and the original path must exist.
10. os.access()
The os.access() function checks whether a specified path is accessible to the current process according to a particular access mode. It can be used to test whether a path exists and whether it can be read, written, or executed.
Python provides several constants for these checks, including F_OK, R_OK, W_OK, and X_OK.
Example
import os
path1 = os.access("Python.txt", os.F_OK)
print("Exist path:", path1)
path2 = os.access("Python.txt", os.R_OK)
print("It access to read the file:", path2)
path3 = os.access("Python.txt", os.W_OK)
print("It access to write the file:", path3)
path4 = os.access("Python.txt", os.X_OK)
print("Check if path can be executed:", path4)
Example Output:
Exist path: False
It access to read the file: False
It access to write the file: False
Check if path can be executed: False
The output depends on whether the specified file exists and what permissions are available for the current process.
Common Python OS Module Functions
| Function or Attribute | Purpose |
|---|---|
os.name | Provides the operating-system-dependent module name. |
os.mkdir() | Creates a new directory. |
os.getcwd() | Returns the current working directory. |
os.chdir() | Changes the current working directory. |
os.rmdir() | Removes an empty directory. |
os.popen() | Opens a pipe to or from a command. |
os.close() | Closes an operating-system file descriptor. |
os.rename() | Renames a file or directory. |
os.access() | Checks accessibility of a specified path. |
Why Use the Python OS Module?
The os module is useful when Python programs need to work with the operating system rather than only processing data inside the application. It provides a standard interface for several common system-level operations.
Developers can use it to create and remove directories, identify the current working directory, change directories, rename files, check path permissions, and interact with operating-system resources. These capabilities make the module useful for automation scripts, file-management programs, development tools, and system-related applications.
Conclusion
The Python os module provides an important connection between Python applications and the operating system. It includes functions and attributes that make it possible to perform many common file, directory, path, and system-related operations directly from Python.
In this guide, we explored os.name, os.mkdir(), os.getcwd(), os.chdir(), os.rmdir(), os.popen(), os.close(), os.rename(), and os.access(). Understanding these features provides a strong foundation for writing Python programs that interact with files, directories, and the underlying operating system.
Complete Advance AI Topics: Click Here
SQL Tutorial: Click Here
YT:- DecodeIT
FAQs
1. What is the OS module in Python?
The Python os module is a standard library module that provides functions for interacting with the operating system, files, directories, paths, and system resources.
2. How do you import the OS module in Python?
You can import the module using the following statement:
import os
3. What does os.getcwd() do?
os.getcwd() returns the current working directory of the Python process.
4. How can you create a directory using Python?
The os.mkdir() function can be used to create a new directory:
import os
os.mkdir("newdir")
5. How do you change the current directory in Python?
The os.chdir() function changes the current working directory to the specified location.
6. What is os.rmdir() used for?
os.rmdir() removes an empty directory from the specified path.
7. What is the difference between os.close() and a file object’s close()?
os.close() is designed to close an operating-system file descriptor, such as one returned by os.open(). A normal Python file object should be closed using its close() method or a context manager.
8. What does os.rename() do?
os.rename() changes the name or path of a file or directory when the operation is permitted by the operating system.
9. What is os.access() used for?
os.access() checks whether a specified path exists or whether the current process has particular types of access, such as read, write, or execute access.
10. Does Python require a separate installation for the os module?
No. The os module is included in Python’s standard library and can normally be imported directly with import os.
Keywords: Python OS Module, os module in Python, import os Python, Python os module example, Python os module documentation, Python os module cheat sheet, Python os module functions, Python os module for Linux, Python os listdir, os module in Python tutorial, Python OS functions