Python

Python OS module Common Methods

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
print(os.name)

Output


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
os.mkdir("Test")
#creating a Test directory in Downloads directory
os.mkdir("/home/linuxhint/Downloads/Test1")

Output

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
os.rmdir("Test")
#removing a Test directory from Downloads directory
os.rmdir("/home/linuxhint/Downloads/Test1")
print("The specified directories are removed successfully")

Output

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())

Output


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())

Output

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.
os.remove("TestFile.txt")
#removing a file from the downloads directory
os.remove("/home/linuxhint/Downloads/TestFile1.txt")
print("The files are removed successfully")

Output

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
print(os.listdir())
#listing down the files and subdirectories of Downloads directory
print(os.listdir("/home/linuxhint/Downloads"))
#listing down the files and subdirectories of Desktop directory
print(os.listdir("/home/linuxhint/Desktop"))

Output

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:

os.rename(“old_name”,new_name)

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
os.rename("student.xml","mystudent.xml")
#renaming the file in Downloads directory
os.rename("myfile.txt","file.txt")
print("The files are renamed successfully")

Output

Conclusion

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.

About the author

Kamran Sattar Awaisi

Kamran Sattar Awaisi

I am a software engineer and a research scholar. I like to write article and make tutorial on various IT topics including Python, Cloud Computing, Fog Computing and Deep Learning. I love to use Linux based operating systems.