.rmdir() method allows the user to delete a folder if it exists in the system or computer and does not contain other folders or files.
FileNotFoundError is raised if the directory is not found.
This method is provided by the
os module which must be imported beforehand.
import os os.rmdir("folder_name") os.rmdir("folder_path")
.rmdir() method works in the following ways:
"folder_name"that will be successfully deleted if the folder exists in the current directory.
"folder_path"can also be passed if it exists outside of the current directory.
Files can be removed using the
.rmdir() to delete the
import os# Delete folder in current directoryos.rmdir("myfolder")# Delete folder in another directoryos.rmdir("/path/to/myfolder")
In this example the
./project directory is deleted using the
- Anonymous contributorAnonymous contributor15 total contributions
- Christine_Yang265 total contributions
- Anonymous contributorAnonymous contributor2 total contributions
- net137228473814 total contributions
- Anonymous contributorAnonymous contributor9 total contributions