如何在Python中创建一个脚本来分别利用uncompyle6、decompyle3和pycdc.exe工具反编译当前目录下的.pyc文件,并将反编译后的代码保存在同一目录下?
时间: 2024-10-21 21:08:23 浏览: 28
在Python中,你可以编写一个脚本,通过os库来遍历当前目录下的.pyc文件,然后依次调用uncompyle6、decompyle3和pycdc.exe等工具来进行反编译。这里是一个基本的示例:
```python
import os
import subprocess
# 定义反编译函数
def decompile(pyc_file):
# Uncompyle6
uncompyle6_cmd = ['uncompyle6', '-f', pyc_file[:-4] + '.py']
with open(pyc_file[:-4] + '_uncompyle6.py', 'w') as out_file:
subprocess.run(uncompyle6_cmd, stdout=out_file)
# Decompyle3
decompyle3_cmd = ['decompyle3', '-i', pyc_file[:-4] + '.py']
with open(pyc_file[:-4] + '_decompyle3.py', 'w') as out_file:
subprocess.run(decompyle3_cmd, stdout=out_file)
# pycdc.exe (假设你有安装并设置了环境变量)
if 'pycdc.exe' in os.environ['PATH']:
pycdc_cmd = ['pycdc', pyc_file]
with open(pyc_file[:-4] + '_pycdc.py', 'w') as out_file:
subprocess.run(pycdc_cmd, stdout=out_file)
# 获取当前目录
current_dir = os.getcwd()
# 遍历.pyc文件并进行反编译
for filename in os.listdir(current_dir):
if filename.endswith('.pyc'):
full_path = os.path.join(current_dir, filename)
decompile(full_path)
阅读全文