In this Python PyQt5 article we are going to learn How to Open & Save Files in Python PyQt5, so Python is powerful programming language that can be used to develop different types of applications including desktop applications. PyQt5 is popular Python GUI library that allows developers to create graphical user interfaces (GUIs) for desktop applications. In this article we are going to talk how to open and save files in Python PyQt5.
So first let’s start from opening a file in Python PyQt5.
1 2 3 4 5 6 7 8 9 |
from PyQt5.QtWidgets import QFileDialog, QApplication app = QApplication([]) filename, _ = QFileDialog.getOpenFileName(None, "Open File", ".", "Text Files (*.txt);;All Files (*)") if filename: with open(filename, 'r') as file: contents = file.read() print(contents) |
in the above code we have imported QFileDialog and QApplication classes from PyQt5.QtWidgets module. after that we have created an instance of the QApplication class.
After that we have used getOpenFileName method of the QFileDialog class to display the file dialog box.
Now let’s talk about saving a file in PyQt5, for saving a file in PyQt5, we also use the QFileDialog widget. however this time we use getSaveFileName method instead of the getOpenFileName method. this is an example of how to use the QFileDialog widget to save a file:
1 2 3 4 5 6 7 8 |
from PyQt5.QtWidgets import QFileDialog, QApplication app = QApplication([]) filename, _ = QFileDialog.getSaveFileName(None, "Save File", ".", "Text Files (*.txt);;All Files (*)") if filename: with open(filename, 'w') as file: file.write("Welcome to GeeksCoders.com") |
in the above code we have used getSaveFileName method of QFileDialog class to display save file dialog box. this method takes the same four arguments as getOpenFileName method.
If user selects a filename, we open the file using open function and write the string Welcome to GeeksCoders.com to it using the write method.
This is the complete code for this article
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog, QPushButton class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("GeeksCoders.com") # Create Open File button open_button = QPushButton("Open File", self) open_button.setGeometry(50, 50, 100, 30) open_button.clicked.connect(self.open_file) # Create Save File button save_button = QPushButton("Save File", self) save_button.setGeometry(50, 100, 100, 30) save_button.clicked.connect(self.save_file) def open_file(self): filename, _ = QFileDialog.getOpenFileName(self, "Open File", ".", "Text Files (*.txt);;All Files (*)") if filename: with open(filename, 'r') as file: contents = file.read() print(contents) def save_file(self): filename, _ = QFileDialog.getSaveFileName(self, "Save File", ".", "Text Files (*.txt);;All Files (*)") if filename: with open(filename, 'w') as file: file.write("Welcome to GeeksCoders.com") if __name__ == "__main__": app = QApplication([]) window = MainWindow() window.show() app.exec_() |
In this code we have created new class MainWindow that inherits from QMainWindow. after that we have created two buttons in the constructor of the class, one for opening a file and one for saving a file. we set the geometry of each button using the setGeometry method and connect their clicked signal to corresponding method, open_file for the Open File button, and save_file for the Save File button.
open_file method uses getOpenFileName method of the QFileDialog class to display file dialog box. this method takes self argument as parent widget which in this case is MainWindow object itself. if the user selects file, we open the file using the open function and read its contents using the read method. We then print the contents to the console.
save_file method uses the getSaveFileName method of the QFileDialog class to display save file dialog box. if user selects a filename, we open file using the open function and write the string Welcome to GeeksCoders.com to it using the write method.
Run your code and this is the result
Learn More on Python GUI
- How to Create Label in PySide6
- How to Create Button in Python & PySide6
- How to Use Qt Designer with PySide6
- How to Add Icon to PySide6 Window
- How to Load UI in Python PySide6
- How to Create RadioButton in PySide6
- How to Create ComboBox in PySide6
- How to Create CheckBox in Python PySide6
- Responsive Applications with PyQt6 Multithreading
- Event Handling in Python PyQt6