Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python os.unlink() method

Python os.unlink() method

Python File methods Python OS file/directory methods


Overview

The os.unlink() method deletes a file; if the file is a directory, an error is returned.

This method is similar to the remove() .

Syntax

The **unlink()** method syntax is as follows:



os.unlink(path)

Parameters

Return Value

This method has no return value.

Example

The following example demonstrates the use of the unlink() method:

Example

#!/usr/bin/python

-- coding: UTF-8 --

import

os

,

sys

List the directory

print

"Directory: %s"

%

os

.

listdir

(

os

.

getcwd

(

)

)

os

.

unlink

(

"aa.txt"

)

The directory after deletion

print

"The directory after deletion is: %s"

%

os

.

listdir

(

os

.

getcwd

(

)

)

The output of the above program is:



Directory is:

 [ 'a1.txt','aa.txt','resume.doc']

The directory after deletion is:

[ 'a1.txt','resume.doc' ]

Python File methods Python OS file/directory methods