Python os.rmdir() Method

Python File(文件) 方法Python OS File/Directory Methods


Overview

The os.rmdir() method is used to delete the directory at the specified path. It only works when the folder is empty; otherwise, an OSError is raised.

Syntax

rmdir()The method syntax format is as follows:

os.rmdir(path)

Parameters

  • path-- The directory path to delete

Return Value

This method has no return value.

Example

The following example demonstrates the use of the rmdir() method:

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

import os, sys

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

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

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

The output of executing the above program is:

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

Python File(文件) 方法Python OS File/Directory Methods

Other Extensions