使用uncompyle6反编译pyc文件

一、前言

假如我们写的python程序源码不小心丢了,但是现在需要对源码进行修改,前期代码是封装成pyc文件使用的,此时可反编译pyc文件,得到源码后再优化。

二、安装

pip install uncompyle6

三、cmd下查看使用方法:

cmd输入命令uncompyle6 --help
返回以下结果

Usage:
  uncompyle6 [OPTIONS]... [ FILE | DIR]...
  uncompyle6 [--help | -h | --V | --version]

Examples:
  uncompyle6      foo.pyc bar.pyc       # decompile foo.pyc, bar.pyc to stdout
  uncompyle6 -o . foo.pyc bar.pyc       # decompile to ./foo.pyc_dis and ./bar.pyc_dis
  uncompyle6 -o /tmp /usr/lib/python1.5 # decompile whole library

Options:
  -o <path>     output decompiled files to this path:
                if multiple input files are decompiled, the common prefix
                is stripped from these names and the remainder appended to
                <path>
                  uncompyle6 -o /tmp bla/fasel.pyc bla/foo.pyc
                    -> /tmp/fasel.pyc_dis, /tmp/foo.pyc_dis
                  uncompyle6 -o /tmp bla/fasel.pyc bar/foo.pyc
                    -> /tmp/bla/fasel.pyc_dis, /tmp/bar/foo.pyc_dis
                  uncompyle6 -o /tmp /usr/lib/python1.5
                    -> /tmp/smtplib.pyc_dis ... /tmp/lib-tk/FixTk.pyc_dis
  --compile | -c <python-file>
                attempts a decompilation after compiling <python-file>
  -d            print timestamps
  -p <integer>  use <integer> number of processes
  -r            recurse directories looking for .pyc and .pyo files
  --fragments   use fragments deparser
  --verify      compare generated source with input byte-code
  --verify-run  compile generated source, run it and check exit code
  --syntax-verify compile generated source
  --linemaps    generated line number correspondencies between byte-code
                and generated source output
  --encoding  <encoding>
                use <encoding> in generated source according to pep-0263
  --help        show this message

Debugging Options:
  --asm     | -a        include byte-code       (disables --verify)
  --grammar | -g        show matching grammar
  --tree={before|after}
  -t {before|after}     include syntax before (or after) tree transformation
                        (disables --verify)
  --tree++ | -T         add template rules to --tree=before when possible

Extensions of generated files:
  '.pyc_dis' '.pyo_dis'   successfully decompiled (and verified if --verify)
    + '_unverified'       successfully decompile but --verify failed
    + '_failed'           decompile failed (contact author for enhancement)

可以按照以上提供的方法进行调用。

四、反编译一个或多个pyc文件

1. help中案例命令解释说明
uncompyle6 -o . foo.pyc bar.pyc

-o后面可以加上文件路径,代表反编译文件输出的位置,“.”表示输出到当前文件夹
反编译成功后会返回“ # Successfully decompiled file

2. 举例:同时反编译如下图两个pyc文件

使用uncompyle6反编译pyc文件_第1张图片

2.1 操作步骤

(1) 将命令切换至demo目录下;
(2) 然后输入uncompyle6 -o . 12.pyc 34.pyc
(3) 反编译成功后,将显示以下结果。

12.pyc --
34.pyc -- decompiled 2 files: 2 okay, 0 failed
# decompiled 2 files: 2 okay, 0 failed

五、批量反编译

import os
import uncompyle6
from uncompyle6 import decompile_file

def main():
    path = os.getcwd()        
    for root, dirs, files in os.walk(path):
        if root != path:
            break
        for filename in files:
            if filename.endswith('pyc'):
                print(filename)
                os.system('uncompyle6 -o . %s'%filename)
    
if __name__ == '__main__':
    main()

你可能感兴趣的:(python,进阶,python)