Course Content

Os Module | Python Tutorials For Absolute Beginners In Hindi #79

You may have noticed that since previous few videos our IDE color has changed. I did it because of too much request and bespeaking from the viewers. You can change your's IDE theme too. I have taught you How to do that in our Pycharm installation tutorial.  The link of the Tutorial is given below:

Downloading Python and PyCharm Installation tutorial.

In today's tutorial, our primary focus will be on learning more than implementing. Today we will see the use of the OS module in Python. OS stands for the operating system. We will also learn how we can perform different operating system related tasks using it. With the help of the OS module, we can make such programs that require access to the operating system directly.

What is OS Module?

OS module provides our code with a direct connection to the operating system. We can use its different functions to interact and do activities on our operating system. For example, if we want to create such software that needs to store or access files from a directory or folder, we can use the OS module to perform the task for us. To use OS Module in Python, we have to import it.

import os

Build-in Function in OS Module:-

OS modules have a lot of built-in functions. You can see the list of built-in functions we can run through it by running the following code, also known as object introspection( For more details, visit tutorial #70):

We will discuss some of these main functions here in this Tutorial.

Built-in Functions

Working

print(dir(os))

 

It gives us a list of all the functions the OS module is composed of.

os.getcwd( ):

 

Here cwd is a short form for the current working directory. The function returns us the path of the directory we are currently in. It is important to know about our directory because when we are trying to import a file in python, the compiler searches for it in our current directory.

 

os.chdir( ):

 

it is used in case we want to change our directory. The new path is sent inside the parenthesis. If we want to access some files or folder from some other directory, we can use it.

 

os.listdir( ):

 

If we want to output the names of all the directories at a certain location, we can use this function.

 

os.mkdir( ):

 

To create a new directory or folder. The name is sent as a parameter inside the parenthesis.

 

 

os.makedirs( ):

 

To make more than on directory simultaneously.

os.rename( ):

 

To rename an already existing directory, we use this. We send the current name and new name of our directory as parameters

os.rmdir( ):

 

It deletes an empty directory.

 

os.removedirs( ):

 

We can remove all directories within a directory by using removedirs(). 

 

os.environ.get( ):

 

It will return us the environment variable. The environment variable must be set, or the function will return null.

os.path.join( ):

 

It joins one or more path components. We can join the paths by simply using a + sign, but the benefit of using this function is that we do not have to worry about extra slashes between the components. So less accuracy still provides us with the same result.

os.path.exists( ):

 

It returns us a Boolean value i.e., either true or false. It is used to check whether a path exists or not. If it does, then the output will be true, otherwise false.

os.path.isfile( ):

 

It returns true if the path is an existing regular file.

os.path.isdir( ): 

 

It returns true if the path is an existingdirectory.

Summing Up:-

In this tutorial, we learned about the OS module with Python 3. The main purpose of the OS module is to interact with the operating system. The primary use of the OS module is to create folders, remove folders, move folders, and sometimes change the working directory. OS module has a lot of built-in functions, some of them we've already discussed in this tutorial. You can explore more OS module build-in function by reading its python documentation.

 

Code as described/written in the video


import os
# print(dir(os))
# print(os.getcwd())
# os.chdir("C://")
# print(os.getcwd())
# f = open("harry.txt")
# print(os.listdir("C://"))
# os.makedirs("This/that")
# os.rename("harry.txt", "codewithharry.txt")
# print(os.environ.get('Path'))
# print(os.path.join("C:/", "/harry.txt"))

# print(os.path.exists("C://Program Files2"))
print(os.path.isfile("C://Program Files"))

Comments(1)

sonu007 1 year, 6 months ago
Harry bhai os module k ek video linux operating system k lia bhi bnao bhai

Resources

No downloadable resources for this video. If you think you need anything, please post it in the QnA!

Course Announcements

Any Course related announcements will be posted here