from PyQt5.QtCore import QFile file = QFile('file.txt') if file.open(QFile.ReadOnly | QFile.Text): print('File opened successfully!') else: print('Failed to open file.')
from PyQt5.QtCore import QFile file = QFile('file.txt') if file.open(QFile.WriteOnly | QFile.Text): file.write('Hello World!') file.close() print('Data written to file successfully!') else: print('Failed to open file.')
from PyQt5.QtCore import QFile file = QFile('file.txt') if file.open(QFile.ReadOnly | QFile.Text): data = file.readAll() file.close() print(str(data, encoding='utf-8')) else: print('Failed to open file.')In all of the above examples, PyQt5.QtCore is the package library used.