1. 程式人生 > 實用技巧 >python 反編譯 compileall

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()