python 反编译 compileall

 

使用py_compile 编译文件

import py_compile

py_compile.compile(r'/hehe.py')

py_compile.compile(r'/hehe.py', 'hehe.pyc')

py_compile.compile(r'/Users/huangrensheng/Irving/myProject/test/hehe.py', 'pycdir/hehe.pyc')

 

 

使用compileall  

编译目录: compile_dir

import compileall

compileall.compile_dir('s1')

 

过滤文件

参数

import compileall
import re

compileall.compile_dir('s1',rx=re.compile(r'/\.txt'))

控制递归层数

maxlevels 不需要递归则设成0

import compileall
import re

compileall.compile_dir('s1', maxlevels=0, rx=re.compile(r'/\.txt'))

 

选择要编译的目录

sys.path

import compileall
import sys

sys.path[:] = ['s1', 's2']
print 'sys.path =', sys.path
compileall.compile_path()

 

在命令行中使用compileall

如下使用示例:

$ python -m compileall -h

 

 

删除py

import os
import compileall

def uncompyle6_file(filepath):
    os.remove(filepath)

def uncompyle6_dir(path='.'):
    print os.path.join(".", path)
    dir_list = os.listdir(os.path.join(".",path))
    for i in dir_list:
        suffix = i.rsplit('.')[-1]
        if i == '.idea' or suffix == 'pyc':
            continue

        file_path = os.path.join(path, i)
        if os.path.isdir(file_path):
            uncompyle6_dir(file_path)
        elif suffix == 'py':
            uncompyle6_file(file_path)

uncompyle6_dir('s1')

 

 

 

反编译成py

import os


uncompyle_path = '/Users/huangrensheng/Library/Python/2.7/bin/uncompyle6'
def uncompyle6_file(filepath):
    # os.system('/Users/huangrensheng/Library/Python/2.7/bin/uncompyle6 -o f1.py f1.pyc')
    # print filepath, filepath[:-1]
    sys_c = '{uncompyle_path} -o {file_py} {file_pyc}'.format(uncompyle_path=uncompyle_path, file_py=filepath[:-1], file_pyc=filepath)
    print sys_c
    os.system(sys_c)

def uncompyle6_dir(path='.'):
    dir_list = os.listdir(path)
    for i in dir_list:
        suffix = i.rsplit('.')[-1]
        if i == '.idea' or suffix == 'py':
            continue

        file_path = os.path.join(path, i)
        if os.path.isdir(file_path):
            uncompyle6_dir(file_path)
        elif suffix == 'pyc':
            uncompyle6_file(file_path)

uncompyle6_dir()

 

posted @ 2020-08-14 12:42  陨落&新生  阅读(1168)  评论(0编辑  收藏  举报