from PyQt5.QtCore import QFile file = QFile("example.txt") if file.open(QFile.ReadOnly | QFile.Text): stream = QTextStream(file) content = stream.readAll() print(content) file.close()
from PyQt5.QtCore import QFile file = QFile("example.txt") if file.open(QFile.WriteOnly | QFile.Text): stream = QTextStream(file) stream << "Hello, world!" file.close()In this example, we create a QFile object for the file "example.txt" and open it for writing with QFile.WriteOnly and QFile.Text. We then create a QTextStream object for writing data to the file and use it to write the string "Hello, world!" to the file. Finally, we close the file.