Is os.remove()+os.rmdir() better then shutil.rmtree()

Viewed 1305

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)

1 Answers

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).

Related