Python os.unlink() Method

Python File(文件) 方法Overview


Overview

The os.unlink() method is used to delete a file, and returns an error if the file is a directory.

This method isremove()the same.

Syntax

unlink()The method syntax is as follows:

os.unlink(path)

Parameters

  • path-- the path of the file to be deleted

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 directory
print "The directory is: %s" %os.listdir(os.getcwd())

os.unlink("aa.txt")

# Directory after deletion
print "The directory after deletion is : %s" %os.listdir(os.getcwd())

The output of the above program is:

目录为:
 [ 'a1.txt','aa.txt','resume.doc']
删除后的目录为 : 
[ 'a1.txt','resume.doc' ]

Python File(文件) 方法Python OS File/Directory Methods

Other Extensions