Python implements recursive generation or deletion of a file directory and files

Source: Internet
Author: User

  1. Import Os,errno
  2. #基本工具类
  3. #① recursively generates a folder or file under the path of the input
  4. #② recursively delete the folders and files below the input path
  5. ‘‘‘
  6. Param:dirpath
  7. return:
  8. authorcreated by Wu Yongcong 2017-8-17
  9. Function:remove a input Dirpath and the files/dictionary under it
  10. ‘‘‘
  11. def removedir (Dirpath):
  12. If not Os.path.isdir (Dirpath):
  13. Return
  14. Files = Os.listdir (Dirpath)
  15. Try
  16. For file in Files:
  17. FilePath = Os.path.join (dirpath, file)
  18. If Os.path.isfile (FilePath):
  19. Os.remove (FilePath)
  20. Elif Os.path.isdir (FilePath):
  21. RemoveDir (FilePath)
  22. Os.rmdir (Dirpath)
  23. Except Exception as E:
  24. Print (e)
  25. ‘‘‘
  26. Param:dirpath
  27. Created by Wu Yongcong 2017-8-17
  28. Function:add a input Dirpath and the files/dictionary under it
  29. ‘‘‘
  30. def mkdir_p (Dirpath):
  31. Try
  32. Os.makedirs (Dirpath)
  33. Except OSError as OE:
  34. if Oe.errno = = errno. Eexist and Os.path.isdir (Dirpath):
  35. Pass
  36. Else
  37. Raise

Python implements recursive generation or deletion of a file directory and files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.