Let's study Python

Use `os.rmdir()` in Python to easily delete directories with confidence.

# Python os.rmdir 사용법

To delete a directory in Python, you can use the `os.rmdir()` function from the `os` library. Here is an example of how to use it:

“`python
import os

directory_path = ‘./dir/testtext.txt’

if os.path.exists(directory_path):
os.rmdir(directory_path)
print(‘Directory successfully deleted.’)
else:
print(‘Error: Directory does not exist.’)
“`

In the above example, we first define the directory path that we want to delete. We then use `os.path.exists()` to check if the directory exists. If it exists, we use `os.rmdir()` to remove the directory. If the directory does not exist, an error message is printed.

Remember that `os.rmdir()` is used specifically for deleting directories. If you want to delete a file, you should use `os.remove()` instead. Additionally, you can use `os.path.isfile()` and `os.path.isdir()` to differentiate between files and directories when checking their existence.

By following this example and understanding how to use `os.rmdir()`, you can effectively delete directories in Python.