程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python creates a visual GUI interface to automatically classify and manage files with one click!

編輯:Python

Often messy folders will make us unable to find the files we want , So a specially made 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

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

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

Recommended reading

  • 5 That's ok Python Code crawling 3000+ Information of listed companies

  • Python Office automation : The strongest and most detailed in the whole network PDF Document operation manual !

  • 50 That's ok Python Code crawl black silk Meimei high definition picture


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved