Python3 os.remove() Method

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


Overview

The os.remove() method is used to delete the file at the specified path. If the specified path is a directory, an OSError will be raised.

Valid on Unix and Windows

Syntax

remove()The method syntax is as follows:

os.remove(path)

Parameters

  • path-- The file path to be removed

Return Value

This method has no return value.

Example

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

#!/usr/bin/python3

import os, sys

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

# 移除
os.remove("aa.txt")

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

Executing the above program produces the following output:

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

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

Other Extensions