Python3 os.unlink() Method

Python3 OS 文件/目录方法Python3 OS File/Directory Methods


Overview

The os.unlink() method is used to delete a file. If the file is a directory, an error is returned.

Syntax

unlink()The 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.

Examples

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

#!/usr/bin/python3

import os, sys

# 列出目录
print ("目录为: %s" %os.listdir(os.getcwd()))

os.unlink("aa.txt")

# 删除后的目录
print ("删除后的目录为 : %s" %os.listdir(os.getcwd()))

Executing the above program produces the following result:

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

Python3 OS 文件/目录方法Python3 OS File/Directory Methods

Other Extensions