Python-文件修改器

#-*- coding: utf-8 -*-

import os
import sys
import glob

from PyQt4.QtGui import *
from PyQt4.QtCore import *

class Change(QWidget):
    def __init__(self, parent = None):
        super(Change, self).__init__(parent)

        self.setWindowTitle(u"文件更改器")
        self.setWindowFlags(Qt.WindowMinimizeButtonHint)
        self.setFixedSize(200, 200)

        btnFile = QPushButton(u"修改文件")
        btnDir = QPushButton(u"修改文件夹")

        QObject.connect(btnFile, SIGNAL("clicked()"), self.changeFile)
        QObject.connect(btnDir, SIGNAL("clicked()"), self.changeDir)

        hbox = QHBoxLayout()
        hbox.addWidget(btnFile)
        hbox.addWidget(btnDir)

        self.setLayout(hbox)

        self.center()
    def center(self):
        screenGeometry = QDesktopWidget()
        windowGeometry = self.size()

        self.move((screenGeometry.width() - windowGeometry.width()) / 2, (screenGeometry.height() - windowGeometry.height()) / 2)

    def doChange(self, fileName, append = "0"):
        with open(fileName, "ab") as f:
            f.write(append)

    def changeDir(self):
        dirName = QFileDialog.getExistingDirectory(self, u"请选择要修改的文件夹")
        if not dirName.isEmpty():
            for fileName in glob.glob(dirName + os.sep + "*"):
                if not os.path.isdir(fileName):
                    self.doChange(fileName)

    def changeFile(self):
        fileName = QFileDialog.getOpenFileName(self, u"请选择要修改的文件")
        if not fileName.isEmpty():
            self.doChange(fileName)

app = QApplication(sys.argv)
change = Change()
change.show()
sys.exit(app.exec_())

a

posted on 2015-07-15 17:41  John_ABC  阅读(539)  评论(0编辑  收藏  举报

导航