Python os.remove() Method

Python File(文件) 方法Python 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.

This method is the same asunlink()the same.

Works on Unix and Windows

Syntax

remove()The method syntax format is as follows:

os.remove(path)

Parameters

  • path-- The path of the file to be removed

Return Value

This method has no return value.

Example

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

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os, sys

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

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

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

The output of the above program execution is:

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

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

Other Extensions