I made a python function, that checks whether a flash USB is inserted and if yes, it deletes all files from the USB.
using OS module here
import os def clean_USB(): try: os.chdir("F:") # change directory to F: (USB in this case) except FileNotFoundError: # "FileNotFoundError" when the program didn't find that directory try: retry = input("USB flash is not inserted. Insert USB and press [ENTER] to try again or [CTRL + C] to exit: ") clean_USB() # run the funtion again (look for F:) if the user retried. except KeyboardInterrupt: # [Ctrl + C] = "KeyboardInterrupt" quit() # close the program otherwise if os.listdir() != : # if the list of all files in USB directory is not an empty list for deletable_file in os.listdir(): # get every file from directory os.remove(deletable_file) # delete that file print("nPrevious PDF files are deleted from USB.") # send message else: print("nUSB flash is already empty.")
But I get an error:
PermissionError: [WinError5] Access denied '$RECYCLE BIN'
I serached about this a little and couldn’t find how to solve the problem, when the program deletes files…
Thank you for any help!
Source: Windows Questions