New Tutorials:   JAVASCRIPT    SASS/SCSS    PL/SQL  
CLOSE
   Python  Python os Module  
   Technology    Programming

OS module in Python - Part 1

         
 SEPTEMBER 24, 2019   by SmritiSatyan

The OS module in Python provides methods using which one can interact with the operating system. This module comes under the standard utility modules in Python. The various functionalities that depend on the operating system can be taken care of with the help of this module.

Sometimes, it is required to automate the creation of certain folders or directories, change the path to folders or get the current directory in which the application is saved (and later perform operations in the directory or path), for such use cases, we can use the os module.

To use the methods of the os module in our python script, we need to import it and then only we can use its methods. To import the os module in your python script, add the following line of code,

import os

In today's post, we will understand the following basic methods from the os module:

  1. The mkdir method

  2. The chdir method

  3. The getcwd method

  4. The rmdir method

  5. The listdir method




1. The mkdir method

This method helps us to create a new directory. Once the os module is imported, the mkdir method takes in the path where the directory has to be created as a parameter. For example,

import os

os.mkdir("path\\to\\my_new_folder")

This method doesn't outputs anything, but when the path (passed as a parameter) is examined, a new folder with the provided name would have been created. If no path is passed as a parameter to the method, it gives a TypeError:

TypeError: mkdir() missing required argument 'path' (pos 1)



2. The chdir method

Suppose you wish to change to a different directory from the one you are currently working in. This can be done using the chdir method. To this method, the path where you wish to navigate to, is passed as the parameter. Let's take a code example,

import os

os.chdir("path\\to\\different\\directory")

This causes the control to go to the new directory. If no path is passed to the chdir method, a TypeError is raised.

TypeError: chdir() missing required argument 'path' (pos 1)

The directory path specified to the chdir method can be relative.




3. The getcwd method

Sometimes, it is required to confirm whether the above method (chdir) has successfully moved on to the new directory or not. This can be checked with the help of getcwd method. This method gives the path to the current working directory. This method doesn't take any parameters. Following is the syntax of using this method,

os.getcwd()

The output would be the path of the directory in which the user is currently in. Passing parameter to this method causes a TypeError:

TypeError: getcwd() takes no arguments (1 given)

Note: To move out of the current directory, the .. can be used as a parameter to the chdir method. For example,

os.getcwd()
# to go out one level from the current directory
os.chdir("..")
os.getcwd()

The first line of code gets the current working directory where the user is currently. The next line of code takes us to the outer directory (one directory up) and the third line proves that using .. with chdir indeed goes one directory back in the system.


Time for an example:

Assume that the new folder is in C:\Users\Studytonight

os.chdir("C:\\Users")
os.chdir("Studytonight")
os.getcwd()

Output:

C:\\Users\\Studytonight



4. The rmdir method

The rmdir method is used to delete or remove a specific directory. When the absolute or relative path to that specific directory has been passed as a parameter, the directory is deleted.


Time for an example:

Note: Run every line of code and go to that specific location and observe the changes.

# Create a new folder in the path "C:\Users\Studytonight\Desktop\new"
os.mkdir("C:\\Users\\ Studytonight \\Desktop\\new")
print(os.getcwd())
os.rmdir("C:\\Users\\ Studytonight \\Desktop\\new")

Output:

'C:\\Users\\ Studytonight\\Desktop\\new'

The first line of code creates a new directory by the name new. The next line prints the directory in which the user is currently in. The third line deletes the newly created directory with name new.




5. The listdir method

Last but not least, the listdir method. This method fetches all the files present in the current directory (wherein the user is working).

os.listdir()

This gives us all the files present in the current directory in the form of a list.

Note: Run these methods in your IDE and observe the output they give.




Conclusion

In today's post, we discussed how various methods from the os module can be used to create, delete and changing the working directories in an operating system.


RELATED POSTS



Subscribe and receive amazing posts directly in your inbox.