python中怎么刪除非空目錄,針對這個問題,這篇文章詳細介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
#!/usr/bin/env python3 # _*_coding:utf-8_*_ # Auth by raysuen import os,sys def ForceRMDir(DirName): ret = 0 try: os.rmdir(os.path.abspath(DirName)) except FileNotFoundError as e: print(e) ret = 1 except OSError as e: for i in os.listdir(os.path.abspath(DirName)): if os.path.isfile("%s/%s"%(os.path.abspath(DirName),i)): os.remove("%s/%s"%(os.path.abspath(DirName),i)) else: ForceRMDir("%s/%s"%(os.path.abspath(DirName),i)) except Exception as e: print(e) return 2 finally: if ret == 0: os.rmdir(os.path.abspath(DirName)) return ret
關(guān)于python中怎么刪除非空目錄問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司行業(yè)資訊頻道了解更多相關(guān)知識。