What does the os.rmdir() function do in Python?

What does the os.rmdir() function do in Python?

a) Deletes an empty directory from the filesystem
b) Deletes a file from the filesystem
c) Renames a directory
d) Copies a directory to a new location

Answer:

a) Deletes an empty directory from the filesystem

Explanation:

The os.rmdir() function in Python is used to delete an empty directory from the filesystem. If the directory contains any files or subdirectories, os.rmdir() will raise an OSError.

import os

# Example of deleting an empty directory
os.rmdir("empty_directory")

In this example, the os.rmdir() function deletes the directory named empty_directory, assuming it is empty.

Removing directories programmatically is useful when cleaning up temporary directories, deleting old or unused directories, or managing directory structures in applications.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top