Python OS ModuleLast Updated : 3 Jan 2026 Python OS module provides the facility to establish the interaction between the user and the operating system. It offers many useful OS functions that are used to perform OS-based tasks and get related information about the operating system. The OS module comes under Python's standard utility modules. This module offers a portable way of using operating system-dependent functionality. It lets us work with the files and directories. To work with the OS module, we need to import the OS module. There are some functions in the OS module, which are given below: 1. os.name()This function provides the name of the operating system module that it imports. Currently, it registers 'posix', 'nt', 'os2', 'ce', 'java', and 'riscos'. Python os.name() Module ExampleLet us take an example to demonstrate the os.name() module in Python. Output: nt 2. os.mkdir()The os.mkdir() function is used to create a new directory. Python os.mkdir() Module ExampleLet us take an example to demonstrate the os.mkdir() module in Python. It will create a new directory in the path in the string argument of the function in the D drive named folder newdir. 3. os.getcwd()It returns the current working directory (CWD) of the file. Python os.getcwd() Module ExampleLet us take an example to demonstrate the os.getcwd() module in Python. Output: C:\Users\Python\Desktop\ModuleOS 4. os.chdir()The os module provides the chdir() function to change the current working directory. Python os.chdir() module ExampleLet's consider an example to demonstrate the os.chdir() module in Python. Output: d:\\ 5. os.rmdir()The rmdir() function removes the specified directory with an absolute or relative path. First, we have to change the current working directory and remove the folder. Python os.rmdir() Module ExampleLet us take an example to demonstrate the os.rmdir() module in Python. 6. os.error()The os.error() function defines the OS level errors. It raises OSError in case of invalid or inaccessible file names and paths, etc. Python os.error() module ExampleLet us take an example to demonstrate the os.error() module in Python. Output: Problem reading: Python.txt 7. os.popen()This function opens a file or one specified by the command specified, and it returns a file object that is connected to a pipe. Python os.popen() Module ExampleLet us take an example to demonstrate the os.popen() module in Python. Output: This is awesome 8. os.close()This function closes the associated file with descriptor fr. Python os.close() Module ExampleLet us take an example to demonstrate the os.close() module in Python. Output: Traceback (most recent call last): File "main.py", line 3, in file = open(fr, 'r') FileNotFoundError: [Errno 2] No such file or directory: 'Python1.txt' 9. os.rename()A file or directory can be renamed by using the function os.rename(). A user can rename the file if it has the privilege to change the file. Python os.rename() Module ExampleLet us take an example to demonstrate the os.rename() module in Python. Output:
Traceback (most recent call last):
File "main.py", line 3, in
os.rename(fd,'Python1.txt')
FileNotFoundError: [Errno 2] No such file or directory: 'python.txt' -> 'Python1.txt'
10. os.access()This function uses real uid/gid to test if the invoking user has access to the path. Python os.access() Module ExampleLet us take an example to demonstrate the os.access() module in Python. 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 ConclusionIn this tutorial, we learnt about the OS module in Python. The Python OS module provides the facility to establish the interaction between the user and the operating system. We can import the OS module by import os. We studied various types of functions in the OS module, such as os.name(), os.mkdir(), os.getcwd(), os.close, etc, with their examples to understand the topic deeply. Next TopicPython Random module |
We request you to subscribe our newsletter for upcoming updates.