How to use Python OS Module?

OS Python Module provides a simple way of using the operating system-dependent functionality. The Python OS Module let us read or write file, manipulate paths, read the lines in a file, creating temporary files and directories and many other functionalities.

Introduction of the Python OS Module:-

Sometimes we need to use the functionalities of the system that we are using. The Python OS Module helps us make it possible. The Python OS Module provide us a way to interact with the operating system-dependent functionalities. 

What does the Python OS Module Do?

It can help us to use the functionalities of the underlying operating system in a portable and easy-to-use way. A good example of the use of the OS Module will be if we want to use it to find the path of the current directory that we are working in. We could use the Python OS module to do it for us.

How to import the Python OS Module?

We can import it with the import statement just like any other module. We can use three ways to import the Python OS module. 


# import as an alias
import  os as OperatingSys
# import one method from os
from os import open
# normal import
import os

How to get the Current Working Directory with Python OS Module?

To get the current working directory we can use the Python OS Module which will help us find the name of the current working directory. Below is the example code for getting the current working directory. it will print the relative path of the current working directory. you can see the code and output of the code in the below code. 


# normal import
import os
# get the current directory
curDir=os.getcwd()
# print the current Diretory
print(curDir)

### Output ####
# D:\Coding_Programs\_01 Python\PythonWork5thSemester\AlixaProDev

How to create a Folder with Python OS Module?

We can use the Python OS Module to create a Folder or Directory in a specified location. We will pass the name of the Folder/directory along with the path. Like if we want to create a directory in the "D" drive with the name of the "alixaprodev" we will pass the whole path like  this "D:\alixaprodev" this will create a directory with the name of the alixaprodev in the D Drive. Below is the code which shows the above statement. 



import os
# this will create a directory
# in D Drive with the name of 
# "Alixa"
os.mkdir("D:\\Alixa")

# Remember #
# it will give you an error if this 
# directory is already present


How to Delete a Folder with Python OS Module?

The Python OS module can also give us the facility to remove or delete a directory permanently. Remember when we normally remove or delete a directory or folder this goes to the recycle bin but this is not the case with the python os module. so be careful when you are deleting or removing a directory. Another point thing is you can not delete the current working directory. we should pass the relative or absolute path of the specified directory. 



import os
# this will Remove a directory
# in D Drive with the name of
# "Alixa"
os.rmdir("D:\\Alixa")

# Remember #
# it will give you an error if this
# directory is not present


How to return the list of the folder with the Python OS Module?

To return the list of all the directories that are available in a directory you can use the listdir() function. it takes a single parameter and returns a list of the folder aka Directories. check the below code.



import os
dirs=os.listdir("D:\\")
for dir in dirs:
    print(dir)

what is the difference between os.mkdir() and os.mkdirs()

there is only one difference between the os.mkdir() and os.mkdirs(). The os.mkdirs() will create the intermediate directories as well like if we want to create a directory "H" inside the Directory "F" so we could do it with both the methods.

with the os.mkdir() ,

we should first create the "F" directory and then inside the "F" directory, we will create the "H" directory just like below.



import os
# first create the F directory inside D
intermediate = os.mkdir("D:\\F")
# second create the H directory inside the F
dirs = os.mkdir("D:\\F\\H")

with the os.mkdirs()

we can create the intermediate "F" directory by calling just the os.mkdirs() method in the following format. look at the example code.



import os
dirs=os.makedirs("D:\\F\\H")

The ultimate result will be the same for both of the codes.

what is the difference between os.rmdir() and os.rmdirs()

Here Again there is only one difference is os.rmdir() and os.rmdirs(). The os.rmdirs() will remove the intermediate directories as well like if we want to remove a directory "H" inside the Directory "F" so we could do it with both the methods.

with the os.rmdir()

we should first remove the "H" directory and then the "F" directory just like below.



import os
# first remove the H directory inside F
dirs = os.rmdir("D:\\F\\H")
# second remove the F directory inside the D
intermediate = os.rmdir("D:\\F")



with the os.rmdirs()

we can remove the intermediate "F" directory by calling just the os.mkdirs() method in the following format. look at the example code.



import os
dirs=os.rmdirs("D:\\F\\H")

The ultimate result will be the same for both of the codes.

How to rename a directory with the Python OS module?

We can rename a directory by calling the rename method. below is the code which rename the "alixaprodev" to "ali".




import os
os.rename("D:\\AlixaProDev","D:\\Ali")

What do the os.walk() methods do?

For each directory in the directory tree rooted at top, it returns a tuple. It traverses all the directories recursively.



import os
d=os.walk("D:\\")
for l in d:
    print(l)



How to print the name of the Operating system-dependent module with Python?

To print the name of the operating system-dependent module we can use the os module. Look at the blow code it will print the name of the operating system-dependent module.



import os
d=os.name
print(d)

how to check if a path exists or not with python?

To check if a path exists or not we can use the python os module. which will help us find the solution. will return true if exists or false if not exists. check the below code.



import os
d=os.path.exists("D:\\")
print(d)

How to use Python OS Module?

Summary and Conclusion:-

We learned about how we can use the Python OS Module and different methods available in the OS module in python. There are still things that are remaining about the OS module. TO learn more about the OS Module you can always access the official documentation of the Python OS Module. If you are interested in other python tutorials please visit my youtube channel Code with Ali.

Post a Comment

0 Comments