Introduction to Python OS Module
Python OS module is a collection of functions that allows a developer to interact with the Operating System using Python Language. This module works with every Operating System and has a list of 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 have preloaded functions that we can call or invoke easily, and when we execute a single function, there are numerous lines of python code that are executed behind the scene, for that single function to work as intended.
- Basically, a module is collections 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 in 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 with a simple example of a function that prints the name of the OS family.
This command will print the name of the family, 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.
import os print(os.name)
As seen, the output is that the function returns the IEEE standard name of the OS.
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.
import os print(os.getcwd())
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 a directory. We can pass a simple path under which we intend to check the list.
import os print(os.listdir('/home'))
It is used when we need to know with what user have we logged in currently. When we run this command, it will return the name of the user logged in. Simply, the name of the user account will be returned and no other details. No parameters are required for this function.
import os print(os.getlogin())
It is a function of the OS module which returns the process ID of the current process. There can be a number of process 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.
import os print(os.getpid())
This function is used when we wish to check the ID for the user that we are currently logged in with. No parameters are required for this function.
import os print(os.getuid())
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.
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 we can even pass a different path. We have to pass a string argument, which is supposed to be the name of the directory.
import os os.mkdir("/home/sulaksh/Desktop/qwerty")
Output over the shell is nothing unless it’s an error. If you will check the file manager, there will be a newly created folder with qwerty as its name.
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.
import os os.rename("/home/sulaksh/Desktop/qwerty", "/home/sulaksh/Desktop/newqwerty" )
When will check the file manager, we will see that the directory we created in the last example is now changed to newqwerty as a result of this command.
Our next function is to delete a directory. The rmdir() function is used to delete or remove a directory. We have to pass a string value as an argument, which is to be the name of a directory to be deleted. Upon successful execution, the function will return nothing, but it will return the error if it fails.
import os os.rmdir("/home/sulaksh/Desktop/newqwerty")
Finally, we will see about an interesting command that the OS module supports. This function returns with 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 an list of objects.
import os os.uname()
As you can see, the return is a collection of five objects: the name of OS, name of the machine, release details of the OS, version details of the OS, and finally, the hardware configuration.
These are the functions provided into the OS module for a python programming language; these functions can be used for a specific purpose and are easy to execute.
Python’s OS Module works as an interface between the Python Programming Language and the Host Operating System. Making operations like creating, deleting, renaming a directory or checking the user that we are logged in with and many such are possible with the OS module. We can simply run these functions within the python shell or write and execute a script.
This is a guide to Python OS Module. Here we discuss the working along with a list of python OS module function respectively. You may also have a look at the following articles to learn more –