Create a visual GUI interface with Python and realize automatic classification and management of files with one click!

AI technology base 2022-05-14 15:34:26 阅读数:64

createvisualguiinterfacepython

91da67151015077e8c008277e1060378.gif

author | Xinyi  

source | Python Enthusiast concentration camp

Often messy folders will make us unable to find the files we want , So Xiaobian specially made a visualization GUI Interface , By inputting the path and clicking with one click, the file can be archived by category .

Different file suffixes are classified into different categories

Let's start by listing a few types of documents , Set according to the suffix of the file , As follows :

SUBDIR = {
    "DOCUMENTS": [".pdf", ".docx", ".txt", ".html"],
    "AUDIO": [".m4a", ".m4b", ".mp3", ".mp4"],
    "IMAGES": [".jpg", ".jpeg", ".png", ".gif"],
    "DataFile": [".csv", ".xlsx"]
}

The file suffixes listed above are not comprehensive , Readers can add... According to their own needs , You can classify according to your preference , Then we customize a function , Judge which class it belongs to according to the input file suffix .

def pickDir(value):
    for category, ekstensi in SUBDIR.items():
        for suffix in ekstensi:
            if suffix == value:
                return category

For example, the input is .pdf The one who returns is DOCUMENTS This class . We also need to customize another function , Traverse all files in the current directory , Get the suffix of many files , Move these files with different suffixes into different categories of folders , The code is as follows :

def organizeDir(path_val):
    for item in os.scandir(path_val):
        if item.is_dir():
            continue
        filePath = Path(item)
        file_suffix = filePath.suffix.lower()
        directory = pickDir(file_suffix)
        directoryPath = Path(directory)
        #  New folder , If the folder does not exist
        if directoryPath.is_dir() != True:
            directoryPath.mkdir()
        filePath.rename(directoryPath.joinpath(filePath))

output

533e7d1d6909ef1c914e1b869364854b.png

Let's build on that again , Seal it again and make it Python Of visualization GUI Interface , The code is as follows :

class FileOrgnizer(QWidget):
    def __init__(self):
        super().__init__()
        self.lb = QLabel(self)
        self.lb.setGeometry(70, 25, 80, 40)
        self.lb.setText(' Folder collation assistant :')
        self.textbox = QLineEdit(self)
        self.textbox.setGeometry(170, 30, 130, 30)
        self.findButton = QPushButton(' Arrangement ', self)
        self.findButton.setGeometry(60, 85, 100, 40)
        self.quitButton = QPushButton(' sign out ', self)
        self.quitButton.clicked.connect(self.closeEvent)
        self.findButton.clicked.connect(self.organizeDir)
        self.quitButton.setGeometry(190, 85, 100, 40)
        self.setGeometry(500, 500, 350, 150)
        self.setWindowTitle('Icon')
        self.setWindowIcon(QIcon('../751.png'))
        self.show()
    def pickDir(self, value):
        for category, ekstensi in SUBDIR.items():
            for suffix in ekstensi:
                if suffix == value:
                    return category
    def organizeDir(self, event):
        path_val = self.textbox.text()
        print(" Path is : " + path_val)
        for item in os.scandir(path_val):
            if item.is_dir():
                continue
            filePath = Path(item)
            fileType = filePath.suffix.lower()
            directory = self.pickDir(fileType)
            if directory == None:
                continue
            directoryPath = Path(directory)
            if directoryPath.is_dir() != True:
                directoryPath.mkdir()
            filePath.rename(directoryPath.joinpath(filePath))
        reply = QMessageBox.information(self, " complete ", " Task to complete , Would you like to exit ?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
        if reply == QMessageBox.Yes:
            event.accept()
        else:
            event.ignore()
    def closeEvent(self, event):
        reply = QMessageBox.question(self, ' sign out ',
                                     " Definite exit ?", QMessageBox.Yes |
                                     QMessageBox.No, QMessageBox.No)
        if reply == QMessageBox.Yes:
            event.accept()
        else:
            event.ignore()

The effect is shown below

dce8d8c166f23fa3f1b48f2fe8c4e657.gif

Finally, we passed pyinstaller Module to put Python Package the code into an executable file , The operation instructions are as follows

pyinstaller -F -w  file name .py

Some parameters have the following meanings :

  • -F: Represents the generation of a single executable

  • -w: Remove the console window , This is in GUI The interface is always very useful

  • -i: Icon representing the executable file

8494d0e8b5ff8a2853a4e94ae9c44a8e.gif

 Past review information transformation 「 poison 」 Apple ? The world's first DMP Loophole
information
From the ancestor level to the Cenozoic , Developer's secret to success
Technology Python String formatting output in
technology 10 An interesting Python Advanced scripts !
Share
Point collection
A little bit of praise
Click to see 
版权声明:本文为[AI technology base]所创,转载请带上原文链接,感谢。 https://pythonmana.com/2022/134/202205141526032174.html