Python os.unlink() method
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
-
path -- The path of the file to delete
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
"Directory: %s"
%
os
.
listdir
(
os
.
getcwd
(
)
)
os
.
unlink
(
"aa.txt"
)
The directory after deletion
"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' ]