Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python3 os.rmdir() Method

Python3 os.rmdir() Method

Python3 OS File/Directory Methods Python3 OS File/Directory Methods


Overview

The os.rmdir() method deletes the directory at the specified path. It only works when the folder is empty; otherwise OSError is raised.

Syntax

The **rmdir()** method syntax is as follows:


os.rmdir(path)

Parameters

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

# List directory
print ("目录为: %s"%os.listdir(os.getcwd()))

# Delete path
os.rmdir("mydir")

# List renamed directories
print ("目录为: %s" %os.listdir(os.getcwd()))

The output of the above program is:


Directory is:
[  'a1.txt','resume.doc','a3.py','mydir' ]
Directory is:
[  'a1.txt','resume.doc','a3.py' ]

Python3 OS File/Directory Methods Python3 OS File/Directory Methods