SoFunction
Updated on 2024-11-15

Python implementation of a one-click automatic file classification management

Often cluttered folders will not allow us to find the files we want, so I have created a specialVisual GUI interfaceThe file is sorted into different categories by typing the path and clicking on it.

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.pdfThe return is thenDOCUMENTSThis class. We also need to customize another function to iterate through all the files in the current directory, get 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 (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

Let's build on it again and encapsulate it into aPython(used form a nominal expression)Visual GUI interfaceThe 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:
            ()

The effect is shown below

Finally we passpyinstallermodule to convert thePythonThe code is packaged into an executable file with the following operating 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: Indicates that the console window is removed, which is done in theGUIThe interface is very useful when
  • -i: Icon representing an executable file

to this article on Python to achieve a key to automatically classify the management of the file is introduced to this article, more relevant Python management file content, please search for my previous articles or continue to browse the following related articles I hope that you will support me in the future more!