반응형

파일들 혹은 폴더를 불러온 후,
파일에 중복된 띄어쓰기가 있다면 1개로 치환해주는 GUI 프로그램이다.
 

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import sys, time, math, os
import multiprocessing as mp
 
try:
    from PyQt5.QtWidgets import QApplication, QMainWindow, QAction, QLabel, QPushButton
    from PyQt5.QtGui import QIcon, QFont, QPainter, QPen, QColor, QPixmap
    from PyQt5.QtCore import Qt, QTimer, QRect
    from PyQt5.QtWidgets import QFileDialog, QMessageBox, QInputDialog
    from PyQt5.QtCore import Qt, QCoreApplication
except:
    from PySide6.QtWidgets import QApplication, QMainWindow, QAction, QLabel, QPushButton
    from PySide6.QtGui import QIcon, QFont, QPainter, QPen, QColor, QPixmap, QAction
    from PySide6.QtCore import Qt, QTimer, QRect
    from PySide6.QtWidgets import QFileDialog, QMessageBox, QInputDialog
    from PySide6.QtCore import Qt, QCoreApplication
QApplication.setAttribute(Qt.AA_EnableHighDpiScaling, True)
 
# From ChatGPT
def reduce_spaces_with_newline(input_file, output_file):
    with open(input_file, 'r'as f:
        lines = f.readlines()
 
    # 각 줄에서 중복된 스페이스 바를 1개의 스페이스 바로 대체하고 줄바꿈 유지
    reduced_lines = [' '.join(line.split()) + '\n' for line in lines]
 
    with open(output_file, 'w'as f:
        f.writelines(reduced_lines)
 
class Tester(QMainWindow):
 
    def __init__(self):
        super().__init__()
        self._widget = None  # No external window yet.
 
        button = self._get_button('Open Folder', parent=self, geo=10,  1014030],  callback=self._open_folder)
        self._button_open_folder = button
        button = self._get_button('Open Files',  parent=self, geo=[160,  1014030],  callback=self._open_files)
        self._button_open_files = button
        button = self._get_button('Clear Files', parent=self, geo=10,  5014030], callback=self._clear_files)
        self._button_clear_files = button
        button = self._get_button('Translate!',  parent=self, geo=[160,  5014030], callback=self._translate)
        self._button_translate = button
        self._button_translate.setDisabled(True)
 
        
        button = self._get_button('Exit',        parent=self, geo=[16013014030], callback=QCoreApplication.instance().quit)
        # self._button_exit = button
 
        self.label = QLabel("Standby"self)
        self.label.setGeometry(109029030)
        font = self.label.font()
        font.setPointSize(10)
        self.label.setFont(font)
 
        self.path = ""
        self.files = list()
 
        self.setWindowTitle("Space Terminator")
        # self.setGeometry(100,300,210,200)
        self.resize(310170)
        self.show()
 
    def _get_length_of_file_list(self):
        return len(self.files)
 
    # Load files in folder
    def _open_folder(self):
        self.label.setText("Open folder")
        folder_name = QFileDialog.getExistingDirectory(self"Select Folder")
        if(len(folder_name) != 0):
            self.path = folder_name
            files = os.listdir(folder_name)
            self._get_files(files)
        
    # Load files selected by user
    def _open_files(self):
        self.label.setText("Open files")
        file_names = QFileDialog.getOpenFileNames(self'Open Files''''Text Files (*.txt);;Log Files (*.log);;All Files(*)')
        files = file_names[0]
        self._get_files(files)
 
    def _get_files(self, files):
        # print("Total {} : ".format(len(files)), files, )
        if(len(files) == 0):
            print("No Selected")
        else:
            self.label.setText("Open {:} files".format(len(files)))
            self.files = list()
            for idx in range(len(files)):
                folder_path, file_name = os.path.split(files[idx])
                extension = file_name[-3:]
                # print(extension)
                if(extension != 'txt' and extension != 'log'):
                    continue
                self.files.append(file_name)
                # print(file_name)
                if(len(folder_path) != 0):
                    self.path = folder_path
            self._button_translate.setDisabled(False)
        # print("Path : ", self.path)
        self._button_translate.setDisabled(False)
 
    def _clear_files(self):
        self.label.setText("Clear files")
        self.files.clear()
        self._button_translate.setDisabled(True)
 
    def _translate(self):
        if(len(self.files) == 0):
            QMessageBox.critical(self"Error""No file list!")
            self.label.setText("ERROR : No file list!")
        else:
            print(self.path)
            for file_name in self.files:
                reduce_spaces_with_newline(file_name, self.path + '/trans_'+file_name)
            self.label.setText("Translate {:} files".format(len(self.files)))
 
    def _get_button(self, name, parent, geo, callback=None):
        button = QPushButton(name, parent)
        button.setGeometry(geo[0],geo[1],geo[2],geo[3])
        if(callback != None): button.clicked.connect(callback)
        return button
 
if __name__ == "__main__":
 
    app = QApplication(sys.argv)
    tester = Tester()
    app.exec()
 
cs

 
asdf

728x90

+ Recent posts