Python 如何从多个.py文件中获取字节码?
我是编程新手,正在从事一个为Python探索字节码的项目。我有几个扩展名为.py的python文件,我想使用Python 如何从多个.py文件中获取字节码?,python,bytecode,Python,Bytecode,我是编程新手,正在从事一个为Python探索字节码的项目。我有几个扩展名为.py的python文件,我想使用dis.dis()函数来保存相当于许多小程序的字节码 例如,下面的代码在运行时将返回函数的字节码(在def部分中)。我的问题是,我无法找到一个解决方案,将文件作为文本/代码导入它们定义的所有函数,并在数千个示例上运行此代码 import dis def something(): n = int(raw_input()) if n % 2 == 0: pr
dis.dis()
函数来保存相当于许多小程序的字节码
例如,下面的代码在运行时将返回函数的字节码(在def
部分中)。我的问题是,我无法找到一个解决方案,将文件作为文本/代码导入它们定义的所有函数,并在数千个示例上运行此代码
import dis
def something():
n = int(raw_input())
if n % 2 == 0:
print (n, "is even")
else:
print (n, "is odd")
dis.dis(something)
有人有什么解决办法吗?我想要一个循环或类似的东西,将.py文件作为文本拉入,粘贴到def中,然后运行代码,保存结果。我可以找出运行和保存的部分,只是在def
中找不到任何可以从.py文件中“粘贴”代码的内容
更新
根据建议(非常感谢!),我尝试了以下代码。我使用了其他的问题和答案,试图解决这个问题。代码运行没有错误,甚至执行了最后一个print语句,并且生成了pyc文件,但是,我在捕获dis.dis(code\u obj,file=out)
输出时遇到了问题,它甚至没有打印或保存。你对我的错误有什么建议吗
import py_compile
from os import walk
# Define the path to compile from
mypath = 'C:/mydirectorycomeshere/SourceCodeChecker/'
# Assign the list of filenames in the mypath folder
f = []
for (dirpath, dirnames, filenames) in walk(mypath):
f.extend(filenames)
break
print('List read')
# Compile all files to pyc
py_compile.main(filenames)
# Define the path where these will be compiled
comppath = 'C:/mydirectorycomeshere/__pycache__/'
# Create new list with compiled files
f2 = []
for (dirpath, dirnames, filenames) in walk(comppath):
f2.extend(comppath)
break
print('Compiled list ready')
# Create bytecode from complied files and save them
import marshal, sys
import dis
import os
from io import StringIO
folderpath = r'C:/mydirectorycomeshere/__pycache__/'
filepaths = [os.path.join(folderpath, name) for name in os.listdir(folderpath)]
all_files = []
# Set header for marshal read of pyc files based on version
header_size = 8
if sys.version_info >= (3, 6):
header_size = 12
if sys.version_info >= (3, 7):
header_size = 16
print('Header set up ready')
# Iterate through compiled files and save or print output
import glob
for filename in glob.glob('*.pyc'):
with open(os.path.join(os.cwd(), filename), 'r') as f:
metadata = f3.read(header_size)
code_obj = marshal.load(f3)
with StringIO() as out:
dis.dis(code_obj, file=out)
print(out.getvalue())
file.write(out)
file = f.readlines()
all_files.append(file)
print('Program ready')
在主管的帮助下,我设法找到了解决办法。这并不考虑特殊情况,例如文件名中的空格,但通过将字节码保存在
.txt
文件中,确实解决了问题:
from os import walk
import glob
import os
# Define the path to compile from
mypath = 'C:/yourfoldercomeshere/'
# Assign the list of filenames in the mypath folder
f = []
for (dirpath, dirnames, filenames) in walk(mypath):
f.extend(filenames)
break
print(filenames)
#Iterate through files, dis them and save them:
for filenames in glob.glob('*.py'):
outfilename = filenames.replace('py', 'txt')
print(outfilename)
filenames=("python -m dis {0} > {1}".format(filenames,outfilename))
os.system(filenames)
print(outfilename)
使用
os.walk()
或glob.glob()
枚举文件,compile()
将其转换为字节码,并使用dis.dis()
对其进行反汇编。谢谢@AKX,我将尝试最后让您知道结果。感谢您的快速响应。您使用的是python 3.*还是2.7?@12944qwerty,我使用的是python 3.8.FYI,def
s定义函数,而不是模块。在Python中,每个.py脚本在技术上都是一个模块。