python 删除非空目录

点击(此处)折叠或打开

  1. #!/usr/bin/env python3
  2. # _*_coding:utf8_*_
  3. # Auth by raysuen
  4. import os,sys
  5. def ForceRMDir(DirName):
  6.     ret = 0
  7.     try:
  8.         os.rmdir(os.path.abspath(DirName))
  9.     except FileNotFoundError as e:
  10.         print(e)
  11.         ret = 1
  12.     except OSError as e:
  13.         for i in os.listdir(os.path.abspath(DirName)):
  14.             if os.path.isfile(“%s/%s”%(os.path.abspath(DirName),i)):
  15.                 os.remove(“%s/%s”%(os.path.abspath(DirName),i))
  16.             else:
  17.                 ForceRMDir(“%s/%s”%(os.path.abspath(DirName),i))
  18.     except Exception as e:
  19.         print(e)
  20.         return 2
  21.     finally:
  22.         if ret == 0:
  23.             os.rmdir(os.path.abspath(DirName))
  24.         return ret
    原文作者:raysuen
    原文地址: http://blog.itpub.net/28572479/viewspace-2157561/
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞