directory - Delete multiple directories in python -
in python, understand can delete multiple files same name using following command eg:
for f in glob.glob("file_name_*.txt"): os.remove(f)
and single directory can deleted shutil.rmtree('/path/to/dir')
- , command delete directory if directory not empty. on other hand, os.rmdir()
needs directory empty.
i want delete multiple directories same name, , not empty. so, looking shutil.rmtree('directory_*')
is there way python?
you have of pieces: glob()
iterates, , rmtree()
deletes:
for path in glob.glob("directory_*"): shutil.rmtree(path)
this throw oserror
if 1 of globbed paths names file, or other reason rmtree()
can fail. can add error handling see fit, once decide how want handle errors. doesn't make sense add error handling unless know want error, have left error handling out.
Comments
Post a Comment