-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileMergerSplitter.py
More file actions
64 lines (51 loc) · 2.47 KB
/
FileMergerSplitter.py
File metadata and controls
64 lines (51 loc) · 2.47 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QFileDialog, QVBoxLayout, QLabel
class FileMerger(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('File Merger/Splitter')
self.setGeometry(760, 440, 400, 100)
self.mergeButton = QPushButton('Merge Files', self)
self.mergeButton.clicked.connect(self.merge_files)
self.splitButton = QPushButton('Split Files', self)
self.splitButton.clicked.connect(self.split_files)
self.resultLabel = QLabel(self)
self.resultLabel.setText('Operation result:')
self.layout = QVBoxLayout()
self.layout.addWidget(self.mergeButton)
self.layout.addWidget(self.splitButton)
self.layout.addWidget(self.resultLabel)
self.setLayout(self.layout)
def merge_files(self):
file_list, _ = QFileDialog.getOpenFileNames(self, "Select the files to be merged", "", "Text Files (*.txt)")
if file_list:
target_file, _ = QFileDialog.getSaveFileName(self, "Select (or create) the merged file", "", "Text Files (*.txt)")
if target_file:
with open(target_file, 'w') as target_way:
for file in file_list:
file_name = file.split('/')[-1]
with open(file, 'r') as f:
subs = f.read().strip()
target_way.write(f"{file_name}\n{'-'*50}\n{subs}\n\n")
self.resultLabel.setText(f'Merged file: {target_file}')
def split_files(self):
file, _ = QFileDialog.getOpenFileName(self, "Select the file to be split", "", "Text Files (*.txt)")
if file:
folder = QFileDialog.getExistingDirectory(self, "Enter the path for the splitted files")
if folder:
with open(file, 'r') as f:
subs = f.read().strip().split('\n\n')
for data in subs:
file_name, subs = data.split('\n' + '-'*50 + '\n')
file_name = file_name.strip()
subs = subs.strip()
with open(f"{folder}/{file_name}", 'w') as target:
target.write(subs)
self.resultLabel.setText(f'Files splited and saved.')
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = FileMerger()
ex.show()
sys.exit(app.exec_())