我将命令行上的可执行文件传递给我的python脚本.我做了一些计算,然后我想将STDIN上的这些计算结果发送到可执行文件.完成后我想从STDOUT获取可执行文件的结果.
ciphertext = str(hex(C1))
exe = popen([sys.argv[1]], stdout=PIPE, stdin=PIPE)
result = exe.communicate(input=ciphertext)[0]
print(result)
Run Code Online (Sandbox Code Playgroud)
当我打印时,result我什么都没有,不是没有,是空行.我确信可执行文件可以处理数据,因为我使用命令行中的">"重复相同的操作,并使用相同的先前计算结果.
Hal*_*ary 15
一个工作的例子
#!/usr/bin/env python
import subprocess
text = 'hello'
proc = subprocess.Popen(
'md5sum',stdout=subprocess.PIPE,
stdin=subprocess.PIPE)
proc.stdin.write(text)
proc.stdin.close()
result = proc.stdout.read()
print result
proc.wait()
Run Code Online (Sandbox Code Playgroud)
得到与" execuable < params.file > output.file" 相同的东西,这样做:
#!/usr/bin/env python
import subprocess
infile,outfile = 'params.file','output.file'
with open(outfile,'w') as ouf:
with open(infile,'r') as inf:
proc = subprocess.Popen(
'md5sum',stdout=ouf,stdin=inf)
proc.wait()
Run Code Online (Sandbox Code Playgroud)