Tuesday 21 September 2021

Python: rmdir: Delete a directory

‘os.rmdir’ method delete a directory. This method delete only empty directory, if the directory contains some files in it, it throws OSError. You will get ‘PermissionError’, if the operation is not permitted.

 

remove_a_directory.py

import os

path = '/Users/Shared/data/emptyDir'
os.remove(path)

 

To delete a directory with content use the method rmtree from shutil module.

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment