【问题标题】:how to import files from command line in python如何在python中从命令行导入文件
【发布时间】:2018-02-22 12:20:59
【问题描述】:

我正在使用 python,我应该从命令行读取文件以进行进一步处理。我的输入文件有一个二进制文件,应该读取它以进行进一步处理。这是我的输入文件 sub.py:

   CODE = " \x55\x48\x8b\x05\xb8\x13\x00\x00"

我的主文件应该是这样的:

import pyvex
import archinfo
import fileinput

import sys
filename = sys.argv[-1]


f = open(sys.argv[-1],"r")
CODE = f.read()
f.close()
print CODE

#CODE = b"\x55\x48\x8b\x05\xb8\x13\x00\x00"
# translate an AMD64 basic block (of nops) at 0x400400 into VEX
irsb = pyvex.IRSB(CODE, 0x1000, archinfo.ArchAMD64())

# pretty-print the basic block
irsb.pp()

# this is the IR Expression of the jump target of the unconditional exit at the end of the basic block
print irsb.next

# this is the type of the unconditional exit (i.e., a call, ret, syscall, etc)
print irsb.jumpkind

# you can also pretty-print it
irsb.next.pp()

# iterate through each statement and print all the statements
for stmt in irsb.statements:
        stmt.pp()

# pretty-print the IR expression representing the data, and the *type* of that IR expression written by every store statement
import pyvex
for stmt in irsb.statements:
  if isinstance(stmt, pyvex.IRStmt.Store):
    print "Data:",
    stmt.data.pp()
    print ""

    print "Type:",
    print stmt.data.result_type
    print ""

# pretty-print the condition and jump target of every conditional exit from the basic block
for stmt in irsb.statements:
        if isinstance(stmt, pyvex.IRStmt.Exit):
                print "Condition:",
                stmt.guard.pp() 
                print "" 

                print "Target:",
                stmt.dst.pp()
                print ""

# these are the types of every temp in the IRSB
print irsb.tyenv.types

# here is one way to get the type of temp 0
print irsb.tyenv.types[0]

问题是当我运行“python maincode.py sub.py”时,它会读取代码作为文件的内容,但它的输出与我直接将代码添加到语句irsb = pyvex.IRSB(CODE, 0x1000, archinfo.ArchAMD64())时完全不同。有谁知道有什么问题,我该如何解决?我什至使用从 inputfile 导入,但它不读取文本。

【问题讨论】:

  • 请分享输出获取并指出您实际期望的输出。

标签: python python-2.7


【解决方案1】:

你考虑过__import__的方式吗?

你可以这样做

mod = __import__(sys.argv[-1])
print mod.CODE

只需将不带 .py 扩展名的文件名作为命令行参数传递:

python maincode.py sub

编辑:显然使用__import__discouraged。相反,尽管您可以使用 importlib 模块:

import sys,importlib
mod = importlib.import_module(sys.argv[-1])
print mod.CODE

..它应该和使用__import__一样工作

如果您需要传递模块的路径,一种方法是在每个目录中添加一个名为

的空文件
__init__.py

这将允许 python 将目录解释为模块命名空间,然后您可以以模块形式传递路径:python maincode.py path.to.subfolder.sub

如果由于某种原因您不能或不想将目录添加为命名空间,并且不想在任何地方添加 init.py 文件,您也可以使用 imp.find_module。你的 maincode.py 会变成这样:

import sys, imp
mod  = imp.find_module("sub","/path/to/subfolder/")
print mod.code

您必须编写代码,将命令行输入拆分为模块部分“sub”和文件夹路径“/path/to/subfolder/”。那有意义吗?一旦它准备好,你就会像你期望的那样调用它,python maincode.py /path/to/subfolder/sub/

【讨论】:

  • 如果我想在命令行上从文件夹输入输入文件怎么办?我试过 sudo python tnt_test.py \test\sub 但它说 mod = __import__(sys.argv[-1]) ImportError : 不支持按文件名导入。我该如何解决这个问题?
  • 查看我编辑的答案。我试图在这里输入它,但一直丢失 init.py 文件周围的下划线到 stackoverflow 的自动格式化......你必须用下划线命名该文件
  • 感谢您的回复。但这对我不起作用。我因错误而停止 __import__(name) ImportError: Import by filename is not supported
  • 您是否尝试了两种文件夹解决方案?无意冒犯,我不想假设任何事情,但是该错误消息听起来像是您仍在尝试仅在没有文件夹的情况下才有效的前两种解决方案。如果您尝试使用点式模块解决方案,您会这样称呼它:'python maincode.py test.sub',因此它不会在传递文件名时出错(因为您不再传递文件名)。如果您尝试了第二种解决方案(您确实传递了文件名),您将不再使用 importlib,而是使用不会出现该错误的 imp 模块。
  • 这两种解决方案都对我有用,所以很可能是沟通不畅。如果您发布或发送给我您最新的代码,这会给您该错误,我们可以找到
【解决方案2】:

您正在以文本形式读取代码,而当以文件形式读取时,您可能以二进制形式读取代码

您可能需要将二进制转换为文本,反之亦然

Binary to String/Text in Python

【讨论】:

  • 这不是二进制,这是十六进制,即使以这种方式转换也不起作用。
猜你喜欢
  • 2010-10-31
  • 2018-07-31
  • 1970-01-01
  • 2013-07-14
  • 1970-01-01
  • 2013-07-06
相关资源
最近更新 更多