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

Python os.rmdir() method

Python os.rmdir() method

Python File methods Python 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/python

# -*- coding: UTF-8 -*-



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

Python File methods Python OS file/directory methods