20 C
Thursday, January 21, 2021

Python OS module Common Methods – Linux Hint

Must read

Python is a popular general-purpose programming language of recent times. It provides many built-in modules and functions to perform specific tasks. Python OS module allows performing the operating system related tasks. The OS module comes pre-installed in Python. The OS modules have many built-in functions to retrieve and interact with the file system. This article explains some functions of the OS module with examples.

os.name() function

The os.name() function returns the operating system name. For example, if you are using Ubuntu, Linux Mint, or any Debian Based operating system, then it will return the “posix” as the operating system name. Let’s use the os.name() function in our Python script.

First, import the os module to use its built-in functions.

#importing the os module
import os
#using os.name function


The os.name() function may returns the different output based on operating system.

os.mkdir() function

The os.mkdir() function makes a new directory. The path and the name of the directory is passed as an argument to the function. Let’s create some test directories.

#importing the os module
import os
#using os.mkdir() function
#creating  a Test directory is current directory
#creating a Test directory in Downloads directory


The directories are created successfully.

os.rmdir() function

The os.mdir() function removes a directory. It takes the name and the path of the file as an argument and removes the directory. The directory must be empty. We cannot remove the current working directory by using os.mdir() function.

#importing the os module
import os
#using os.rmdir() function
#removing  a Test directory
#removing a Test directory from Downloads directory
print(“The specified directories are removed successfully”)


os.getcwd() function

The os.getcwd() function returns the name of the current working directory. The current working directory is that directory where the currently executed Python file is saved. In my case, the Python file being executed is stored in the “Documents” directory. Therefore, my current working directory will be the “Documents” directory. Let’s use the os.getcwd() function in our Python script to get the current working directory information.

#importing the os module
import os
#using os.getcwd
print(“The current working directory is: “,os.getcwd())


Now let’s change the location of the currently executed file from “Documents” to the “Downloads” directory, and you will see that the current working directory path will change.

#importing the os module
import os
#using os.getcwd
print(“The current working directory is: “,os.getcwd())


The current working directory is changed to the “Downloads” directory.

os.remove() function

The os.remove() function removes or deletes a specified file. We specify the name of the file along with the path, and it removes a file. When we remove a file from the current directory, then there is no need to specify the path; while removing the file or directories from the other directories, we specify the path as well. Let’s remove some file by using os.remove() function.

#importing the os module
import os
#using os.remove() function to remove a file from the current directory.
#removing a file from the downloads directory
print(“The files are removed successfully”)


The files are removed successfully.

os.listdir() function

The file system is one of the essential components of any operating system that manages and save the files. The os.listdir() function list down the files and subdirectories of a stated directory. If no directory is mentioned, then it considers the current working directory and returns the files are subdirectories. The result will be returned in the list form. Let’s use the os.listdir() function in our Python script.

#importing the os module
import os
#using os.listdir function
#listing down the files and subdirectories of current working directory
#listing down the files and subdirectories of Downloads directory
#listing down the files and subdirectories of Desktop directory


The output shows the list of files and subdirectories of various directories.

os.rename() function

The os.rename() function changes the name of existing file. The syntax of the os.rename() function is as follows:


The 1st parameter takes the old file name as an argument and in 2nd parameter, we define the new file name. Let’s implement os.rename() function in our Python script.

#importing the os module
import os
#using os.rename function
#renaming the file in current working directory
#renaming the file in Downloads directory
print(“The files are renamed successfully”)



The Python os module is a built-in module that allows performing operating system related tasks. It has many built-in functions to access and manage the files on operating systems. This article explains the os module and its functions in detail.

Source link

- Advertisement -

More articles


Please enter your comment!
Please enter your name here

Latest article