from PyQt5.QtCore import QFile # Copy a file from one location to another source_file = QFile("C:/Users/username/Desktop/source_file.txt") destination_file = QFile("C:/Users/username/Desktop/destination_file.txt") QFile.copy(source_file.fileName(), destination_file.fileName())
from PyQt5.QtCore import QFile, QIODevice # Copy a file using a buffer source_file = QFile("C:/Users/username/Desktop/source_file.txt") destination_file = QFile("C:/Users/username/Desktop/destination_file.txt") if source_file.open(QIODevice.ReadOnly) and destination_file.open(QIODevice.WriteOnly): buffer_size = 1024 data = source_file.read(buffer_size) while len(data) > 0: destination_file.write(data) data = source_file.read(buffer_size) source_file.close() destination_file.close()This example copies the file "source_file.txt" from the location "C:/Users/username/Desktop/" to the location "C:/Users/username/Desktop/" with a new name "destination_file.txt" using a buffer. In both examples, QFile.copy() is used to copy the file from one location to another. The first example uses the simplest method of copying the file by passing the file names as arguments to the method. The second example shows how to copy a file using a buffer by reading and writing small portions of data at a time. Both examples use the PyQt5.QtCore package library.