Small knowledge, big challenge! A python package uncompile6 will be used when you decompile pyC files into PY files

The installation

You can install this by PIP install uncompile6 on the command line or by pycharm. Once installed successfully, it is ready to use

use

Typing uncompile6 on the command line will return you some basic usage

Since there are many project files, I wrote a Python script main function to decompile pyc files into py files

def uncompyle_pyc(filename, newname):
    """
    uncompyle pyc file
    :param filename: pyc file
    :param newname: py file
    :return:
    """
    command = "uncompyle6 -o " + newname + " " + filename
    print(command)
    try:
        os.system(command)
    except Exception as e:
        print("maybe command has Chinese character,notice:command must be English code")
    else:
        print("there is other mistake")
Copy the code

It then iterates through the project, calls this function, and stores the translated file in the same place as the PYC file.

def detect_walk(dir_path):
    """
    uncompyle all pyc in dirs(include child and parent dirs)
    :param dir_path: root dir
    :return:
    """
    for root, dirs, files in os.walk(dir_path):
        print(root, dirs, files)
        for filename in files:
            if ".pyc" in filename:
                newname = root + spd + filename.split(".")[0] + ".py"
                filename1 = root + spd + filename
                print("%s---->%s" % (filename1, newname))
                uncompyle_pyc(filename1, newname)
Copy the code

Python’s os.walk() method is based on a top-down deep traversal method. The parameter is a directory, which is used as the root directory for convenience. Then it reads its files and folders from the first subdirectory of the root directory as the new root directory; Through this sequence, this function returns a tuple (root,dirs,files), as illustrated by a practical example