Python batch Delete Files

Source: Internet
Author: User

Python batch Delete Files

When writing a program for testing, you will always encounter the log to be deleted from the log folder or delete some historical files, which will be generated each time, during the test, you always need to delete these files before the test to find out the cause of the error. It is troublesome to manually delete the files. Therefore, you need to write a batch Delete script.


Import osdef removeFileInFirstDir (targetDir): for file in OS. listdir (targetDir): targetFile = OS. path. join (targetDir, file) if OS. path. isfile (targetFile): OS. remove (targetFile) lists = ['log', 'downloaded'] if _ name _ = "_ main _": for lista in lists: removeFileInFirstDir (lista)

Put the name of the folder to be deleted into the list, and use the for loop to traverse all the folders to be deleted to delete the files in it.


The script and folder must be at the same level.

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.