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檔目錄的方法