Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在正在运行的程序中运行python脚本_Python_Bash_Shell_Sh_Executable - Fatal编程技术网

在正在运行的程序中运行python脚本

在正在运行的程序中运行python脚本,python,bash,shell,sh,executable,Python,Bash,Shell,Sh,Executable,我正在运行一个python脚本,该脚本启动一个名为./abc的可执行文件。该可执行文件进入程序内部,并等待如下命令: $./abc abc > \\waits for a command here. 我想做的是输入两个命令,如: $./abc abc > read_blif alu.blif abc > resyn2 到目前为止,我掌握的情况如下: import os from array import * os.system('

我正在运行一个python脚本,该脚本启动一个名为./abc的可执行文件。该可执行文件进入程序内部,并等待如下命令:

$./abc
abc >                      \\waits for a command here.
我想做的是输入两个命令,如:

$./abc
abc > read_blif alu.blif
abc > resyn2
到目前为止,我掌握的情况如下:

import os
from array import *

os.system('./abc')
for file in os.listdir("ccts/"):
    print 'read_blif ' + file + '\n'
    print 'resyn2\n'
    print 'print_stats\n'
    print 'if -K 6\n'
    print 'print_stats\n'
    print 'write_blif ' + file.split('.')[0] + 'mapped.blif\n'
但是,这将执行以下操作:

abc >                 \\stays idle and waits until I ^C and then it prints
read ...blif
resyn2
...
它只打印到终端。我如何让它在程序内部执行这个命令,并等待它看到下一个abc>来运行下一个命令。
谢谢

我用电脑做了类似的事情


这将执行由
cmd
指定的命令,然后将结果读入
result
。它将等待有一个结果打印到控制台,然后在结果分配之后执行任何操作。我相信这可能是你想要的,尽管你的描述有点模糊

我也使用了类似的方法


这将执行由
cmd
指定的命令,然后将结果读入
result
。它将等待有一个结果打印到控制台,然后在结果分配之后执行任何操作。我相信这可能是你想要的,尽管你的描述有点模糊

如果要将命令导入可执行文件的输入,最简单的方法是使用模块。您可以输入到可执行文件的stdin中,并使用获取其输出

但是,这将每次关闭子进程的stdin,但这是可靠通信而无死锁的唯一方法。根据,您应该使用
pexpect
进行“类似交互式会话”的程序。假设您可以让不同的子进程运行该程序,通过使用多个子进程可以避免这种情况

我假设您只需要
print_stats
stdout
s,因此您可以这样做(例如,您可能希望处理错误):


如果要将命令通过管道传输到可执行文件的输入中,最简单的方法是使用模块。您可以输入到可执行文件的stdin中,并使用获取其输出

但是,这将每次关闭子进程的stdin,但这是可靠通信而无死锁的唯一方法。根据,您应该使用
pexpect
进行“类似交互式会话”的程序。假设您可以让不同的子进程运行该程序,通过使用多个子进程可以避免这种情况

我假设您只需要
print_stats
stdout
s,因此您可以这样做(例如,您可能希望处理错误):


您需要使用
子流程
库生成一个新流程,并创建两个管道,一个用于
stdin
,另一个用于
stdout
。使用这些管道(在python中表示为文件),您可以与流程通信 以下是一个例子:

 import subprocess

cmd = './full/path/to/your/abc/executable'
pro = subprocess.Popen(cmd, stdout=subprocess.PIPE, 
                       stdin=subprocess.PIPE)

pro.stdin.write("read_blif alu.blif \n")
pro.stdout.read(3)
您可以使用
pro.communicate
,但我假设您需要为输入的每个命令获取输出。比如:

abc > command1
ouput1
abc > command2
output2 -part1
output2 -part2
output2 -part3
这样,我认为
管道
方法更有用

使用python中的
dir
函数查找有关
pro
对象的更多信息,以及可用的
dir(proc)
方法和属性。不要忘记内置的
help
,它将显示docstrings
help(pro)
help(pro.stdin)

当你运行操作系统时,你犯了一个错误。这将在后台运行你的程序,你将无法控制它。也许您希望了解什么是输入/输出流。
可以进行更多的阅读。

您需要使用
子流程
库生成一个新流程,并制作两个管道,一个用于
stdin
,另一个用于
stdout
。使用这些管道(在python中表示为文件),您可以与流程通信 以下是一个例子:

 import subprocess

cmd = './full/path/to/your/abc/executable'
pro = subprocess.Popen(cmd, stdout=subprocess.PIPE, 
                       stdin=subprocess.PIPE)

pro.stdin.write("read_blif alu.blif \n")
pro.stdout.read(3)
您可以使用
pro.communicate
,但我假设您需要为输入的每个命令获取输出。比如:

abc > command1
ouput1
abc > command2
output2 -part1
output2 -part2
output2 -part3
这样,我认为
管道
方法更有用

使用python中的
dir
函数查找有关
pro
对象的更多信息,以及可用的
dir(proc)
方法和属性。不要忘记内置的
help
,它将显示docstrings
help(pro)
help(pro.stdin)

当你运行操作系统时,你犯了一个错误。这将在后台运行你的程序,你将无法控制它。也许您希望了解什么是输入/输出流。
可以进行更多阅读。

您可能正在寻找pexpect模块。以下是pexpect文档中的基本示例

# This connects to the openbsd ftp site and
# downloads the recursive directory listing.
import pexpect
child = pexpect.spawn('ftp ftp.openbsd.org')
child.expect('Name .*: ')
child.sendline('anonymous')
child.expect('Password:')
child.sendline('noah@example.com')
child.expect('ftp> ')
child.sendline('lcd /tmp')

我认为,如果您的操作系统与pexpect兼容,abc>也会以同样的方式工作。

您可能正在寻找pexpect模块。以下是pexpect文档中的基本示例

# This connects to the openbsd ftp site and
# downloads the recursive directory listing.
import pexpect
child = pexpect.spawn('ftp ftp.openbsd.org')
child.expect('Name .*: ')
child.sendline('anonymous')
child.expect('Password:')
child.sendline('noah@example.com')
child.expect('ftp> ')
child.sendline('lcd /tmp')

如果你的操作系统与pexpect兼容,我想abc>也会有同样的效果。

你能澄清一下你想要什么吗?你正在跟踪内置的
文件(对于Python3.4(可能是整个Python3),这没问题,但我发现这里不是这样的)。考虑重命名它。当您从cmdline执行abc可执行文件时,它实际上做什么?你得说得更具体些。很抱歉描述得含糊不清,但下面所有的答案都是正确的。我只是想捕获程序的终端输出并向其写入命令。你能澄清一下你想要什么吗?你正在跟踪内置的
文件
(对于Python3.4(可能是整个Python3),这是可以的,但我发现这里不是这样的)。考虑重命名它。当您从cmdline执行abc可执行文件时,它实际上做什么?你得说得更具体些。很抱歉描述得含糊不清,但下面所有的答案都是正确的。我只是想捕获程序的终端输出并向其写入命令。谢谢您的回答。我正在实施你所说的