from PyQt5.QtCore import QFile import os filename = "test.txt" file = QFile(filename) if file.exists(): file.remove() print(f"{filename} deleted successfully.") else: print(f"{filename} does not exist.")
from PyQt5.QtCore import QFile import os dirname = "temp" # iterate over all files in directory for filename in os.listdir(dirname): filepath = os.path.join(dirname, filename) file = QFile(filepath) if file.exists(): file.remove() print(f"{filepath} deleted successfully.") else: print(f"{filepath} does not exist.")Here, we first iterate over all files in the "temp" directory using os.listdir(). For each file, we create a QFile object with the full file path and check if the file exists. If the file exists, we call the remove() function on the QFile object to delete the file, and print a success message. If the file does not exist, we print a message stating that it does not exist. Package library: PyQt5.