Easy Tutorial
❮ Python Requests Python Count Occurrences Element List ❯

Python3 os.rmdir() Method

Python3 OS File/Directory Methods


Overview

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

Syntax

The syntax for the rmdir() method is as follows:

os.rmdir(path)

Parameters

Return Value

This method does not return any value.

Example

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

#!/usr/bin/python3

import os, sys

# List the directory
print ("Directory is: %s"%os.listdir(os.getcwd()))

# Delete the path
os.rmdir("mydir")

# List the directory after renaming
print ("Directory is: %s" %os.listdir(os.getcwd()))

Executing the above program will produce the following output:

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

Python3 OS File/Directory Methods

❮ Python Requests Python Count Occurrences Element List ❯