demand

A teaching assistant directory structure inspection tools, the directory structure is [books] – [chapters] – [questions | answers] – [*.jpg], the background a asynchronous processing service, need strong dependence on the directory structure. Books parsing is a separate pipeline, the log is not visible to the user, here is written in python tools to the user, convenient and they check the directory structure, logic is very simple, check whether all books under section contains only “title”, “the answer” two folders, if there are other file or a directory structure is wrong, need to print out the abnormal structure

Code implementation

import os


def folder_check(path) :
    print("================ Directory structure checking =================")
    folders = os.listdir(path)
    success_files = []
    error_files = []
    for f in folders:
        chapter_path = os.path.join(path, f)
        if os.path.isdir(chapter_path):
            content_dir = os.listdir(chapter_path)
            has_error = False
            for sub_f in content_dir:
                if(sub_f ! ='title') & (sub_f ! =The 'with answers'):
                    error_files.append("================ invalid directory or file: {}/{}".format(f, sub_f))
                    has_error = True

            if not has_error:
                success_files.append("================ directory check passed: {}".format(f))

    if len(error_files) < 1:
        print("...")
        for sf in success_files:
            print(sf)
        print("...")
        print("( ̄▽ ̄) Congratulations, your {} chapter directory structure has been checked and passed".format(len(success_files)))
    else:
        print("...")
        for ef in error_files:
            print(ef)
        print("...")
        print("(; '⌒') sorry, there is a problem with your file directory structure, please check it.")


if __name__ == "__main__":
    Exe can be loaded directly into the book directory and executed directly
    rootPath = r"./"
    folder_check(rootPath)
    input("Press Any Key to close")

Copy the code

Package the exe execution file

Py script execution requires the installation of the Python interpreter, but the installation of the ordinary user is cumbersome (if you want to talk to a female colleague, do not need to package exe), in this case, we can package python script as an exe execution file, double-click to run

Install pyinstaller

Run the following command to wait until the installation is complete:

pip install pyinstaller
Copy the code

packaging

Execute scripts directly

pyinstaller -F -i insp.ico main.py -n 'Directory Inspection Gadget'182 INFO: PyInstaller: 4.2 182 INFO: Python: 3.9.2 182 INFO: Platform: Windows-10-10.0.19041-sp0 182 INFO: PyInstaller: 4.2 182 INFO: Python: 3.9.2 182 INFO: Platform: Windows-10-10.0.19041-sp0 182 INFO: Wrote C:\Users\ XXX \PycharmProjects\pythonProject\Ŀ ▒▒▒ с ▒▒▒▒. Spec 182 INFO: UPX is not available. Extending PYTHONPATH with paths ………… ..................... C: \ Users \ \ XXXX PycharmProjects \ pythonProject \ dist \ Ŀ fell ▒ ▒ ▒ С ▒ ▒ ▒ ▒. Exe 8025 INFO: Building EXE from EXE-00.toc completed successfully.Copy the code

-f stands for direct overwriting of the original file so that each package is the latest -i insp.ico executable file icon, icon download address: www.easyicon.net/ -n Executable file name

Execution effect:The icon is in the upper left corner of the command line