Reputation: 73
What is safer and faster- to first delete entire dirs - with os.remove() and then kill empty dir with os.rmdir()
-OR
just use shutil.rmtree() and kill all in one step ?
(sorry, new to python)
Upvotes: 5
Views: 6573
Reputation: 2292
os.remove()
throws an exception if the file doesn't exist, while shutil.rmtree()
doesn't care if the directory is empty or not. Therefore, it is easier to use the latter in one step, rather than the former in addition to os.rmdir()
(which would ideally require a try-except
block or os.path.isfile()
to ensure the file exists).
Upvotes: 9