Python script File Delete

Source: Internet
Author: User
Tags python script

Yesterday there was a need to delete files in a directory with a Python script. Got into some trouble.

Using the Shutil.rmtree (dir) function, this function can delete the contents of the directory, while Shutil.rmdir (dir) can only delete the empty directory.

1, a delete on the error, the final suspicion is the account permissions issues, change the permissions of the account, you can delete some files, but there are files can not be deleted

2, in the online search may be due to the file attributes of the problem, if not deleted files are read-only. If you want to delete these files, you need to modify their properties first.

Shutil module is not similar to the function, so that their own modeled shutil.rmtree function to write a, before deleting the file before the first os.chmod (file, Stat. S_iwrite) Just a little bit better.

Remember to import the OS, SYS, Stat.

Python does not use much, but some of the permissions problems really took a while to figure it out.

Python script File Delete

Related Article

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.