Python os.rmdir() Method

Advertisements


Description

The method rmdir() removes the directory path. It works only when the directory is empty, else OSError is raised.

Syntax

Following is the syntax for rmdir() method:

os.rmdir(path)

Parameters

  • path -- This is the path of the directory, which needs to be removed.

Return Value

This method does not return any value.

Example

The following example shows the usage of rmdir() method.

# !/usr/bin/python

import os, sys

# listing directories
print "the dir is: %s" %os.listdir(os.getcwd())

# removing path
os.rmdir("mydir")

# listing directories after removing directory path
print "the dir is:" %os.listdir(os.getcwd())

When we run above program, it produces following result:

the dir is: 
[  'a1.txt','resume.doc','a3.py','mydir','Administrator','amrood.admin' ]
os.rmdir("mydir")
OSError: [Errno 90] Directory not empty: 'mydir'

The error is coming as 'mydir' directory is not empty. If 'mydir' is an empty directory, then this would produce following result:

the dir is: 
[  'a1.txt','resume.doc','a3.py','mydir','Administrator','amrood.admin' ]
the dir is: 
[  'a1.txt','resume.doc','a3.py','Administrator','amrood.admin' ]



Advertisements
Advertisements