file = QFile() file.setFileName('example.txt') if file.open(QIODevice.ReadOnly | QIODevice.Text): print("File opened successfully in read-only mode")
file = QFile() file.setFileName('example.txt') if file.open(QIODevice.WriteOnly | QIODevice.Text): stream = QTextStream(file) stream << "Hello, World!"In this example, a QFile object is created, and the setFileName method is called to set the name of the file to 'example.txt'. The file is then opened in write-only mode using the open method with the QIODevice.WriteOnly and QIODevice.Text flags specified. A QTextStream object is then created, which provides a convenient way to write text to a file. The text 'Hello, World!' is then written to the file using the << operator. Overall, the PyQt5.QtCore package library provides a powerful set of tools for working with files in Python.