Python3 os.remove()方法

remove()方法删除文件的路径。如果路径是一个目录,将引发 OSError 异常。

语法

以下是 remove()方法的语法:
os.remove(path)

参数

  • path -- 这是将被删除的路径

返回值

此方法不返回任何值

示例

下面的示例显示 remove()方法的使用。
# !/usr/bin/python3

import os, sys
os.chdir("d:\\tmp")
# listing directories
print ("The dir is: %s" %os.listdir(os.getcwd()))

# removing
os.remove("test.java")

# listing directories after removing path
print ("The dir after removal of path : %s" %os.listdir(os.getcwd()))

当我们运行上面的程序,它会产生以下结果:
The dir is: ['Applicationdocs.docx', 'book.zip', 'foo.txt', 'home', 'Java Multiple Inheritance.html', 'Java Multiple Inheritance_files', 'java.ppt', 'ParallelPortViewer', 'test.java']
The dir after removal of path : ['Applicationdocs.docx', 'book.zip', 'foo.txt', 'home', 'Java Multiple Inheritance.html', 'Java Multiple Inheritance_files', 'java.ppt', 'ParallelPortViewer']

上一篇: Python3文件方法 下一篇: Python3 os文件目录的方法