现在,我有一个测试file.dat,在它上运行十六进制,并将输出放入hexdump.dat文件中。
subprocess.Popen(['hexdump file.dat > hexdump.dat' ], shell=True) 顺便提一句,我已经看到了不使用shell=True的建议,但实际上我得到了错误OSError: [Errno 2] No such file or directory。
因此,我希望能够传递变量或数组、文件,而不是硬编码的"file.dat“。“文件”可以是用户输入,也可以是从前一个子进程部分生成的数组/列表。
我尝试过一个用户输入案例:
from subprocess import Popen, PIPE, STDOUT
files = raw_input('File Name: ')
p = subprocess.Popen(['hexdump files > hexdump.dat' ], stdout=PIPE, stderr=STDOUT)
out,err = p.communicate(input=files) 还包括:
p = subprocess.Popen(['hexdump', inputs, ' > hexdump.dat' ], stdout=PIPE, stderr=STDOUT) 谢谢你的帮助,我知道我不太理解这里所需要的结构,所以一些“手持式”的答案会很感激。
发布于 2016-04-27 21:38:33
您需要shell=True,因为否则它会查找具有该名称的可执行文件。shell=True告诉方法使用shell来执行命令,这样>和朋友就会变成您最初想要的样子(重定向)。
您发布的下列代码:
from subprocess import Popen, PIPE, STDOUT
files = raw_input('File Name: ')
p = subprocess.Popen(['hexdump files > hexdump.dat' ], stdout=PIPE, stderr=STDOUT)
out,err = p.communicate(input=files) 不会工作,因为您只是将files传递给hexdump,而且如果一个名为files的文件不存在,您将得到一个错误(如果它确实存在,它可能仍然不是您想要的)。
您想要构建要执行的字符串:
file = "input.dat"
p = subprocess.Popen("hexdump " + file + " > hexdump.dat", shell=True)发布于 2016-04-27 21:46:03
发布于 2016-04-27 21:38:32
与使用>重定向不同,您可以使用stdout重定向。至于文件列表,您只需将文件列表附加到包含十六进制的数组中,即
myfiles = ['file1','file2']
with open('hexdump.dat', 'w') as output:
proc = subprocess.Popen(['hexdump'] + myfiles, stdout=output)https://stackoverflow.com/questions/36901169
复制相似问题