Python3 os.remove() 方法
概述
os.remove() 方法用於刪除指定路徑的文件。 如果指定的路徑是一個目錄,將拋出OSError。
在Unix, Windows中有效
語法
remove()方法語法格式如下:
os.remove(path)
參數
path --要移除的文件路徑
返回值
該方法沒有返回值
實例
以下實例演示了remove() 方法的使用:
#!/usr/bin/python3 import os, sys # 列出目录 print ("目录为: %s" %os.listdir(os.getcwd())) # 移除 os.remove("aa.txt") # 移除后列出目录 print ("移除后 : %s" %os.listdir(os.getcwd()))
執行以上程序輸出結果為:
目录为: [ 'a1.txt','aa.txt','resume.doc' ] 移除后 : [ 'a1.txt','resume.doc' ]