from PyQt5.QtCore import QFile # Open the file in read mode file = QFile("example.txt") if file.open(QFile.ReadOnly | QFile.Text): # Read the entire contents of the file data = file.readAll() # Convert the QByteArray to a string text = str(data, "utf-8") print(text) file.close()
from PyQt5.QtCore import QFile # Open the file in read mode file = QFile("example.bin") if file.open(QFile.ReadOnly): # Read the entire contents of the file data = file.readAll() # Convert the QByteArray to a bytes object byte_data = bytes(data) print(byte_data) file.close()This code opens a binary file named "example.bin" in read-only mode, then reads its contents using the QFile readAll method. The resulting QByteArray is then converted to a bytes object and printed to the console. Overall, the PyQt5.QtCore package library provides a range of core functionality for PyQt5 applications, including file I/O operations such as QFile readAll.