file = QFile('example.txt') if file.open(QIODevice.WriteOnly): data = 'Hello, World!' file.writeData(data.encode()) file.flush() file.close()
file = QFile('example.txt') if file.open(QIODevice.ReadOnly): data = file.readAll().data().decode() print(data) file.close()This example creates a QFile object for the file 'example.txt' and opens it in read-only mode. It then reads all of the data from the file, decodes it from bytes to a string, prints it to the console, and closes the file. Package library: PyQt5.