Questions And Answers

More Tutorials

Python 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.

Built-in FunctionsWorking
print(dir(os)) It gives us a list of all the functions the OS module is composed of.Here cwd is a short form for the current working directory. The function returns us the path of the directory we are currently in
os.getcwd( ): 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 folders 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 existing directory.

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.


In this page (written and validated by ) you learned about Python Os Module . What's Next? If you are interested in completing Python tutorial, your next topic will be learning about: Python Requests Module For HTTP Requests.

Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue:

Share On:

Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using, you agree to have read and accepted our terms of use, cookies and privacy policy.