Updated May 24, 2023
Introduction to Python OS Module
Python OS module is a collection of functions allowing developers to interact with the Operating System using Python Language. This module works with every Operating System and lists functions and operations that can be executed. Basically, the OS Module within Python Programming Language allows us to execute a number of commands towards our installed operating system. It is also absolutely possible to write a program that will execute a list of various OS commands when invoked.
How does Python OS Module work?
- Python’s OS Module has preloaded functions that we can call or invoke easily. When we execute a single function, multiple lines of Python code are executed behind the scenes to ensure that the function works as intended.
- Basically, a module is a collection of Python language code, which consists of Python functions, variables, and classes.
- The OS module of Python consists of Python code, which is a collection of functions and classes specifically used for interaction with the Operating System.
- There are two basic methods to rub these functions, and both work as per our requirements.
List of Python OS Module Function
We can execute the OS module functions over a Python shell or write a program where we include the required functions.
We will now see a simple example of a function that prints the name of the OS family.
1. os.name
This command will print the family’s name, which the IEEE specifies for OS. We have to import the os module file and then execute the functions. No parameters are required for this function.
Code:
import os
print(os.name)
Output:
As seen, the output is that the function returns the IEEE standard name of the OS.
2. getcwd()
This function returns the name of the current working directory. The current working directory is the directory from where we have currently logged into the Python shell. Upon execution, this function will return a proper path of the current directory. No parameters are required for this function.
Code:
import os
print(os.getcwd())
Output:
3. listdir()
This function is a simple command to check every file and directory present in the current directory. Here the command can be understood as a list directory, meaning the return will be a list of all the directories that are present under our currently logged in directory. We can pass a simple path under which we intend to check the list.
Code:
import os
print(os.listdir('/home'))
Output:
4. os.getlogin()
It is used when we need to know with what user we have logged in currently. When we run this command, it will return the user’s name logged in. The function will return only the name of the user account and does not require any additional parameters.
Code:
import os
print(os.getlogin())
Output:
5. os.getpid()
It is a function of the OS module, which returns the process ID of the current process. There can be a number of processes running in the background, and when we execute this function, we receive an ID for our current process. No parameters are required for this function.
Code:
import os
print(os.getpid())
Output:
6. os.getuid()
To delete or remove a directory, pass a string value as an argument that represents the name of the directory to be deleted.
Code:
import os
print(os.getuid())
Output:
Moving on, we will now demonstrate the working of os module functions for creating a new directory and renaming a directory. Unlike executing a program and receiving proper feedback, we don’t see any return message unless the command fails to execute due to any condition.
Below we have a simple command to create a fresh new directory.
7. mkdir()
It is a basic function from the OS module, which creates a new directory where we execute the command, and we can pass the proper name or even a different path.
Code:
import os
os.mkdir("/home/sulaksh/Desktop/qwerty")
Output:
Output over the shell is nothing unless it’s an error. If you check the file manager, there will be a newly created folder with qwerty as its name.
8. rename()
This function is a simple tool to rename a directory name through the Python shell. We have to pass two string values as an argument, the first has to be a source name, and the second will be the new name with which we intend to rename the directory.
Code:
import os
os.rename("/home/sulaksh/Desktop/qwerty", "/home/sulaksh/Desktop/newqwerty" )
Output:
When we look in the file manager, we’ll notice that the directory established in the previous example has been renamed to “newqwerty” due to running this command.
9. rmdir()
Our next function is to delete a directory. To delete or remove a directory, we use the rmdir() function. To delete or remove a directory, pass a string value as an argument that represents the directory’s name to be destroyed.
Upon successful execution, the function will return nothing, but it will return the error if it fails.
Code:
import os
os.rmdir("/home/sulaksh/Desktop/newqwerty")
Output:
Finally, we will see an interesting command that the OS module supports. This function returns a list of useful information related to the currently logged in Operating System. Over that, the return is a collection of five objects. This function takes no parameters and returns a list of objects.
Code:
import os
os.uname()
Output:
As you can see, the return is a collection of five objects: the name of the OS, the name of the machine, the release details of the OS, the version details of the OS, and finally, the hardware configuration.
These are the functions provided in the OS module for a Python programming language; these functions can be used for a specific purpose and are easy to execute.
Conclusion
Python’s OS Module interfaces the Python Programming Language and the Host Operating System. The OS module allows us to perform operations such as creating, deleting, and renaming a directory, as well as checking the logged-in user and many others. We can simply run these functions within the Python shell or write and execute a script.
Recommended Articles
We hope that this EDUCBA information on “Python OS Module” was beneficial to you. You can view EDUCBA’s recommended articles for more information.