Python os.remove() 方法

Python File(檔) 方法 Python OS 檔/目錄方法


概述

os.remove() 方法用於刪除指定路徑的檔。如果指定的路徑是一個目錄,將拋出OSError。

在Unix, Windows中有效

語法

remove()方法語法格式如下:

os.remove(path)

參數

  • path -- 要移除的檔路徑

返回值

該方法沒有返回值

實例

以下實例演示了 remove() 方法的使用:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

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' ]

Python File(檔) 方法 Python OS 檔/目錄方法