简体   繁体   中英

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

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM