One click to realize automatic classification and management of files. It's really fragrant to make a visual GUI interface with Python

Often disorderly folders will make us unable to find the files we want, so I specially made a visual GUI interface to realize the filing of files by categories by one click through the input path. Like to remember collection, praise and attention.

Different file suffixes are classified into different categories

Let's first list some types of documents, which are set according to the suffix of the document, 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 them according to their own needs and classify them according to their own preferences. Then we can customize a function to judge which class it belongs to according to an input file suffix

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

For example, enter yes pdf returns the DOCUMENTS class. We also need to customize a function to traverse all files in the current directory, obtain the suffixes of many files, and 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)
        # Create a new folder if it does not exist
        if directoryPath.is_dir() != True:
            directoryPath.mkdir()
        filePath.rename(directoryPath.joinpath(filePath))

output

On the basis of this, we will encapsulate the visual GUI interface of Python. 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 organizer 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", "The task is completed. Do you want 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',
                                     "OK to exit?", QMessageBox.Yes |
                                     QMessageBox.No, QMessageBox.No)
        if reply == QMessageBox.Yes:
            event.accept()
        else:
            event.ignore()

The effect is shown in the figure below

Finally, we use the pyinstaller module to package the Python 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: Indicates that the console window is removed, which is very useful in the GUI interface

  • -i: An icon representing an executable

Recommended articles

Tags: Python GUI programming language

Posted by turdferguson on Tue, 17 May 2022 23:20:41 +0300