简体   繁体   中英

Delete Specific file type in bulk

I need to delete files with a specific extension and the script I have does that but it does it one by one but not in bulk and I have honestly been very confused on how to complete this can I get some help on this please the script below:

for root, dirnames, filenames in os.walk('path'):
    for xml in filenames:
        if xml.lower().endswith('.xml'):
            if input('remove exisiting xml files? y/n: ') == "y":
                os.remove(os.path.join(root, XML))
                print('the file has been deleted succesfully')
        else:
            print('the file has not been deleted')

If you are looking for a modification to your script, so you don't have to type 'y' for each file you can do the following:

for root, dirnames, filenames in os.walk('path'):
    for xml in filenames:
        if xml.lower().endswith('.xml'):
            os.remove(os.path.join(root, XML))  

If you do this, be aware that the script will now delete any file ending in.xml

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM