Latest web development tutorials

Python os.rmdir() 方法

Python File(文件) 方法 Python OS文件/目錄方法


概述

os.rmdir() 方法用於刪除指定路徑的目錄。 僅當這文件夾是空的才可以, 否則, 拋出OSError。

語法

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

os.rmdir(path)

參數

  • path --要刪除的目錄路徑

返回值

該方法沒有返回值

實例

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

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

import os, sys

# 列出目录
print "目录为: %s"%os.listdir(os.getcwd())

# 删除路径
os.rmdir("mydir")

# 列出重命名后的目录
print "目录为: %s" %os.listdir(os.getcwd())

執行以上程序輸出結果為:

目录为:
[  'a1.txt','resume.doc','a3.py','mydir' ]
目录为:
[  'a1.txt','resume.doc','a3.py' ]

Python File(文件) 方法 Python OS文件/目錄方法