Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python3 os.remove() Method

Python3 os.remove() Method

Python3 OS File/Directory Methods Python3 OS File/Directory Methods


Overview

The os.remove() method deletes the file at the specified path. If the specified path is a directory, OSError is raised.

Valid on Unix and Windows

Syntax

The **remove()** method syntax is as follows:


os.remove(path)

Parameters

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

# List directory
print ("目录为: %s" %os.listdir(os.getcwd()))

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

# List directories after removal
print ("移除后 : %s" %os.listdir(os.getcwd()))

The output of the above program is:


Directory is:
[ 'a1.txt','aa.txt','resume.doc' ]
After removal :
[ 'a1.txt','resume.doc' ]

Python3 OS File/Directory Methods Python3 OS File/Directory Methods