OS Module in Python Programming
Modules in Python
In this lesson, we will understand what is OS Module in Python Programming and how to implement them along with some examples.
What is OS Module in Python?
The os module contains functions that use the operating system dependent functionality to process operating system dependent tasks like creating a new directory, renaming a file, etc.
Many functions are available in the os module, but we will discuss only those that are frequently used in python programs.
os.name
The os.name returns the operating system's name on which we run our python code. The os name can be posix, nt, os2, ce, java, riscos.
Example
import os
x = os.name
print(x)
Output
nt
Here nt means we are running our python code on Microsoft Windows operating system.
os.listdir()
The os.listdir() function returns the list of all files and directories names present in a specified directory (folder).
Example
import os
x = os.listdir(path='C:\Program Files (x86)\Python\Python38-32')
print(x)
Output
['DLLs', 'Doc', 'include', 'Lib', 'libs', 'LICENSE.txt', 'NEWS.txt', 'python.exe', 'python3.dll', 'python38.dll', 'pythonw.exe', 'Scripts', 'tcl', 'Tools', 'vcruntime140.dll']
os.getcwd()
The os.getcwd() function returns the name of the current working directory. The current working directory is the directory from where we are running our python codes.
Example
import os
x = os.getcwd()
print(x)
Output
C:\Dremendo\PYTHON
In our case, we are running all our python codes from the directory given in the output.
os.chdir()
The os.chdir() function change the current working directory to a specified directory.
Example
import os
x = os.getcwd()
print(x)
os.chdir('D:/templete')
x = os.getcwd()
print(x)
Output
C:\Dremendo\PYTHON D:\templete
os.mkdir()
The os.mkdir() function creates a new directory at a specified path. The specified path must exist to avoid an error.
Example
import os
os.mkdir('D:/Data64/Alpha')
print('Directory Created Successfully')
Output
Directory Created Successfully
In the above example, the Data64 directory must exist at D:\ drive to create the Alpha directory.
os.makedirs()
The os.makedirs() function creates a new directory at a specified path. If the specified path does not exist, then the os.makedirs() function will create it.
Example
import os
os.makedirs('D:/Data64/Alpha/Beta')
print('Directory Created Successfully')
Output
Directory Created Successfully
In the above example, if the Data64 directory does not exist at D:\ drive, then the os.makedirs() function will create the Data64 and all its sub-directories.
os.rename()
The os.rename() function is used to rename a file or directory. The rename() function takes two arguments: the path of the file or directory to be renamed and the new name for the file or directory along with its full path.
Example
import os
os.rename('D:/Data64/Alpha/Beta/delta.txt','D:/Data64/Alpha/Beta/record.txt')
print('File Renamed Successfully')
os.rename('D:/Data64/Alpha/Beta','D:/Data64/Alpha/Data')
print('Directory Renamed Successfully')
Output
File Renamed Successfully Directory Renamed Successfully
os.remove()
The os.remove() function deletes a specified file. The specified file must exist to avoid an error.
Example
import os
os.remove('D:/Data64/Alpha/Data/record.txt')
print('File Deleted Successfully')
Output
File Deleted Successfully
os.rmdir()
The os.rmdir() function deletes an empty directory. The function will raise an error if the directory is not empty.
Example
import os
os.rmdir('D:/Data64/Alpha/Data')
print('Directory Deleted Successfully')
Output
Directory Deleted Successfully
Note: To delete a directory, whether empty or not empty, we can use the function shutil.rmtree(). See the example given below.
Example
import shutil
shutil.rmtree('D:/Data64')
print('Directory Deleted Successfully')
Output
Directory Deleted Successfully
os.system()
The os.system() function executes the system commands.
Example
import os
os.system('date')
Output
The current date is: 30-Aug-22 Enter the new date: (dd-mm-yy)
The above program will execute the date command in the command prompt of the Windows operating system.
Note: We can also clear our console screen in python using the system command cls for windows operating system and clear for Linux and Mac operating systems.
Example
import os
os.system('cls')
The above program will clear the console screen in python under the Windows operating system.
os.startfile()
The os.startfile() function runs or open a file with its default associated application. For example, a microsoft word file will be opened using Microsoft Word application.
Example
import os
os.startfile('D:/Accountancy.pdf')
The above program opened the pdf file Accountancy.pdf, which is in our D: drive.
os.path.basename()
The os.path.basename() function returns the basename of a specified path. The right-most name in a specified path is called the basename.
Example
import os
x = os.path.basename('D:/MySQL/bin/mysql.exe')
y = os.path.basename('D:/MySQL/bin')
print('Path = D:/MySQL/bin/mysql.exe')
print('Basename = %s' %(x))
print('Path = D:/MySQL/bin')
print('Basename = %s' %(y))
Output
Path = D:/MySQL/bin/mysql.exe Basename = mysql.exe Path = D:/MySQL/bin Basename = bin
os.path.dirname()
The os.path.dirname() function returns the full directory path after excluding the basename from the specified path.
Example
import os
x = os.path.dirname('D:/MySQL/bin/mysql.exe')
y = os.path.dirname('D:/MySQL/bin')
print('Path = D:/MySQL/bin/mysql.exe')
print('Directory path = %s' %(x))
print('Path = D:/MySQL/bin')
print('Directory path = %s' %(y))
Output
Path = D:/MySQL/bin/mysql.exe Directory path = D:/MySQL/bin Path = D:/MySQL/bin Directory path = D:/MySQL
os.path.exists()
The os.path.exist() function returns true if the specified path exists. If the specified path does not exist, then it returns false.
Example
import os
x = os.path.exists('D:/MySQL/bin/mysql.exe')
y = os.path.exists('D:/MySQL/bin/data')
print('Path = D:/MySQL/bin/mysql.exe')
print('mysql.exe file exist = %s' %(x))
print('Path = D:/MySQL/bin/data')
print('data directory exist = %s' %(y))
Output
Path = D:/MySQL/bin/mysql.exe mysql.exe file exist = True Path = D:/MySQL/bin/data data directory exist = False