from PyQt5.QtCore import QFile # File path filename = "/path/to/file.txt" if QFile.exists(filename): print("File exists") else: print("File not found")
from PyQt5.QtCore import QDir, QFile # Directory path dirname = "/path/to/dir" # File name filename = "file.txt" # Create a file path filepath = QDir(dirname).filePath(filename) if QFile.exists(filepath): print("File exists") else: print("File not found")This example shows how to use the exists() method to check if a file exists in a directory. It creates a file path by using the QDir.filePath() method and checks if the file exists. If the file exists, it prints "File exists" to the console, otherwise, it prints "File not found".