Python 3 - os.unlink() Method


Description

The method unlink() removes (deletes) the file path.If the path is a directory, OSError is raised. function is identical to remove(); the unlink name is its traditional Unix name.

Syntax

Following is the syntax for unlink() method −

os.unlink(path)

Parameters

path − This is the path, which is to be removed.

Return Value

This method does not return any value.

Example

The following example shows the usage of unlink() method.

# !/usr/bin/python3
import os, sys
os.chdir("d:\\tmp")

# listing directories
print ("The dir is: %s" %os.listdir(os.getcwd()))
os.unlink("foo.txt")

# listing directories after removing path
print ("The dir after removal of path : %s" %os.listdir(os.getcwd()))

Result

When we run the above program, it produces the following result −

The dir is: [
   'Applicationdocs.docx', 'book.zip', 'foo.txt', 
   'Java Multiple Inheritance.htm', 'Java Multiple Inheritance_files', 
   'java.ppt', 'python2'
]

The dir after removal of path : [
   'Applicationdocs.docx', 'book.zip', 
   'Java Multiple Inheritance.htm', 
   'Java Multiple Inheritance_files', 'java.ppt', 'python2'
]
python_files_io.htm
Advertisements