I need to delete files with a specific extension but IT HAS TO TAKE THE USERS INPUT 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 I have been struggling, the script below I appreciate all help:
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')
Why not asking first the input question once for all the files, and then deleting every file after confirmation? For example:
import os
if input('remove exisiting xml files? y/n: ') != "y":
print('Canceling deleting process')
else:
print('deleting all xml files')
for root, dirnames, filenames in os.walk('path'):
for xml in filenames:
if xml.lower().endswith('.xml'):
file_path = os.path.join(root, xml)
print(f'deleting file {file_path}')
os.remove(file_path)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments