os.unlink() method in Python with examples

Hello coders, in this tutorial we will learn os.unlink() method of Python with examples. Before we go ahead, we need to have some knowledge about OS module.

OS module in Python helps using the operating system dependent functionalities. Moreover, all the functions in this module raise OSError exception in case of invalid parameters or filename, the path. Some functionalities by OS module for files and directories are:

os.stat (path, dir_fd=None): This operation tells about the status of a file whose path is given.

os.listdir (path): Returns a list of names of entries in the directory or path specified.

os.chdir (path): It changes the directory to the path specified.

os.link (src, dst): This operation creates the link to src named dst.

os.mkdir(pathmode=0o777*dir_fd=None): Make directory at the specified path and mode.

os.unlink (path,dir_fd=None): Delete the file path .

os.rename (src, dst): This operation renames the directory or file from src to dst. 

In this tutorial, we will have a detailed discussion about os.unlink() method in python.

os.unlink() in Python from OS module

os.unlink() method removes or deletes the path. However, it raises IsADirectory error when a path to a directory is pass as an argument. In order to remove the directory use os.rmdir().

Syntax:
os.unlink(path,dir_fd)

Parameters of os.unlink():
path: file path or path-like object name.[path-like object which is an object referring file path.]
dir_fd : File descriptor referring to the directory, its default value is None.

Return type: This method does not return any value.

Likewise, os.remove() has functionalities similar to os.unlink().

Below are the code samples for os.unlink() :

Code 1
The code below removes the symtab.txt file from the Documents directory.

import os #import os module

os.unlink('/home/rs/Documents/symtab.txt')
print('Successfully removed the file')

Output for the above code:

Successfully removed the file

 

Code 2
The code below shows that we can store the path of the file to be removed in a variable and pass the variable as a parameter to os.unlink().

import os
path='/home/rs/Documents/pooltab.txt' #We can store the filepath in a variable and the unlink(remove) it

os.unlink(path)
print('Successfully removed the file')

Output for the above code:

Successfully removed the file

 

Code 3
The code below shows that when we try to remove a directory using os.unlink it gives IsADirectoryError. To clarify, we can’t remove a directory using os.unlink().

import os

os.mkdir('/home/rs/Documents/pooltab1',mode=777)  # Making a directory

os.unlink('/home/rs/Documents/pooltab1') #this will give Is A Directory error
print('Successfully removed the file')

Output for the above code:

--------------------------------------------------------------------------- 
IsADirectoryError Traceback            (most recent call last) 
<ipython-input-11-ac22ad785a13> in <module>
 ----> 1 os.unlink('/home/rs/Documents/pooltab1') 
         2 print('Successfully removed the file') 
IsADirectoryError: [Errno 21] Is a directory: '/home/rs/Documents/pooltab1'

 

In conclusion, os.unlink is used to remove the file path.

Leave a Reply

Your email address will not be published. Required fields are marked *