SoFunction
Updated on 2024-11-19

python make visual GUI interface automatic classification management file

Often cluttered folders will make us can not find the desired file, so I deliberately created a visual GUI interface, by entering the path of one click to achieve the file sorting and filing.

Different file suffixes are grouped into different categories

Let's start by listing the broad categories of files, which are set according to the file's suffix, roughly 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 is not comprehensive, the reader can add according to their own needs to the inside, according to their own preferences to be divided into categories, and then we customize a function, based on the input of a file suffix to determine which category it belongs to

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

For example, the input is .pdf return is DOCUMENTS this class. We also need to customize a function, traversing the current directory of all the files, access to a number of file extensions, these different extensions of the file were moved into different categories of folders, the code is as follows

def organizeDir(path_val):
    for item in (path_val):
        if item.is_dir():
            continue
        filePath = Path(item)
        file_suffix = ()
        directory = pickDir(file_suffix)
        directoryPath = Path(directory)
        # Create a new folder, if it doesn't already exist
        if directoryPath.is_dir() != True:
            ()
        ((filePath))

output

We again on top of the foundation, and then encapsulated into a Python visualization of the GUI interface The code is as follows

class FileOrgnizer(QWidget):
    def __init__(self):
        super().__init__()
         = QLabel(self)
        (70, 25, 80, 40)
        ('Folder Organization Assistant:')
         = QLineEdit(self)
        (170, 30, 130, 30)
         = QPushButton('Organize', self)
        (60, 85, 100, 40)
         = QPushButton('Exit', self)
        ()
        ()
        (190, 85, 100, 40)
        (500, 500, 350, 150)
        ('Icon')
        (QIcon('../'))
        ()
    def pickDir(self, value):
        for category, ekstensi in ():
            for suffix in ekstensi:
                if suffix == value:
                    return category
    def organizeDir(self, event):
        path_val = ()
        print("The path is: " + path_val)
        for item in (path_val):
            if item.is_dir():
                continue
            filePath = Path(item)
            fileType = ()
            directory = (fileType)
            if directory == None:
                continue
            directoryPath = Path(directory)
            if directoryPath.is_dir() != True:
                ()
            ((filePath))
        reply = (self, "Finish.", "Mission complete, do you wish to exit?",  | , )
        if reply == :
            ()
        else:
            ()
    def closeEvent(self, event):
        reply = (self, 'Exit',
                                     "Sure about quitting?",  |
                                     , )
        if reply == :
            ()
        else:
            ()

rendering (visual representation of how things will turn out)

Finally, we use the pyinstaller module to package the Python code into an executable file with the following instructions

pyinstaller -F -w filename.py

The meanings of some of the parameters are as follows:

-F: indicates generation of a single executable file

-w: Remove the console window, which is very useful in the GUI interface.

-i: an icon indicating an executable file

The above is python make visual GUI interface automatic classification management file details, more information about python visual GUI automatic classification management please pay attention to my other related articles!