概述
os.remove() 方法用於刪除指定路徑的文件。如果指定的路徑是一個目錄,將拋出OSError。
在Unix, Windows中有效
語法
remove()方法語法格式如下:
os.remove(path)
參數
-
path -- 要移除的文件路徑
返回值
該方法沒有返回值
實例
以下實例演示了 remove() 方法的使用:
#!/usr/bin/python # -*- coding: UTF-8 -*- import os, sys dirPath = "test/" print '移除前test目錄下有文件:%s' %os.listdir(dirPath) #判斷文件是否存在 if(os.path.exists(dirPath+"foo.txt")): os.remove(dirPath+"foo.txt") print '移除后test 目錄下有文件:%s' %os.listdir(dirPath) else: print "要刪除的文件不存在!"
執行以上程序輸出結果為: