Python method rmdir() removes the directory path. It works only when the directory is empty, else OSError is raised.
Following is the syntax for rmdir() method:
os.rmdir(path)
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 the above program, it produces the 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 the 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' ]
Here at Intellinuts, we have created a complete Python tutorial for Beginners to get started in Python.