Python deletes an instance of a non-empty folder

Source: Internet
Author: User
Below for you to share a Python Delete non-empty folder instances, has a good reference value, I hope to be helpful to everyone. Come and see it together.

Generally delete files using the OS library, and then use Os.remove (path) to complete the deletion, if you delete the empty folder can use Os.removedirs (path),

However, if you need to delete the entire folder, and the folder is not empty when using os.removedirs (path) will be an error, you can use the Shutil library, which is a python built-in library, is a file and folder advanced operation of the library, can be complementary to the OS Library to complete some operations, such as the overall copy of the folder, move the folder, rename the file and so on.

Import osimport shutilos.remove (Path) #删除文件os. Removedirs (Path) #删除空文件夹shutil. Rmtree (path) #递归删除文件夹

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.