批量删除特定文件类型

Delete Specific file type in bulk

我需要删除具有特定扩展名的文件,我的脚本会这样做,但它会一个一个地删除,但不会批量删除,老实说,我对如何完成此操作感到非常困惑,我可以得到一些帮助吗请使用以下脚本:

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')

如果您正在寻找对脚本的修改,这样您就不必为每个文件键入 'y',您可以执行以下操作:

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

如果您这样做,请注意脚本现在将删除任何以 .xml

结尾的文件