Python3 os.rmdir() Method

Python3 OS 文件/目录方法Python3 OS File/Directory Methods


Overview

The os.rmdir() method is used to delete the directory at the specified path. This only works if the folder is empty; otherwise, it raises an OSError.

Syntax

rmdir()The method syntax is as follows:

os.rmdir(path)

Parameters

  • path-- The directory path to be deleted

Return Value

This method has no return value.

Example

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

#!/usr/bin/python3

import os, sys

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

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

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

The output of the above program is:

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

Python3 OS 文件/目录方法Python3 OS File/Directory Methods

Other Extensions