python - 如何将命令作为在 python 子进程内启动的 shell 的输入

标签 python input command subprocess

我想创建一个 GUI python 脚本来启动多个进程。所有这些进程最初都是通过用perl脚本(start_workspace.perl)设置一个shell来调用的,并在shell下键入可执行文件名。 在start_workspace.perl里面,它首先设置一些ENV变量,然后调用exec(/bin/bash),它启动shell,所以你可以在提示符下输入“execfile”来启动。

我的问题是,从我的python脚本来看,我仍然想使用这个shell(通过subprocess.popen("perl start_workspace.perl")),但我不想被阻止手动输入“execfile”。我希望我可以在调用“start_workspace.perl”的步骤中指定“execfile”,并且该过程可以在没有任何干预的情况下完成。

类似命令重定向的东西。但不知道是否可行。

subprocess.popen(("perl start_workspace.perl") < "execfile")

最佳答案

你们很接近。在 subprocess documentation ,参见:

stdin, stdout and stderr specify the executed programs’ standard input, standard output and standard error file handles, respectively. Valid values are PIPE, an existing file descriptor (a positive integer), an existing file object, and None. PIPE indicates that a new pipe to the child should be created. With None, no redirection will occur; the child’s file handles will be inherited from the parent. Additionally, stderr can be STDOUT, which indicates that the stderr data from the applications should be captured into the same file handle as for stdout.

stdin、stderr 和 stdout 是 popen 方法的命名参数。您可以打开输入文件并将其文件描述符作为标准输入传递给新进程。

关于python - 如何将命令作为在 python 子进程内启动的 shell 的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3806784/

相关文章:

python - 如何让机器人框架等到数据出现在MySQL表上才继续测试用例执行

python - 为什么这个用于分布式计算的 Python 0MQ 脚本会在固定输入大小时挂起?

linux - 如何使用/dev/stdin 和 read.csv() 从终端读取输入?

linux - 使用 bash 时忽略正则表达式

macos - Mac 的 cmake 安装程序无法创建/usr/bin 符号链接(symbolic link)

python - 多重矩阵乘法

python - 如何通过批处理文件测试python脚本是否成功运行

javascript - &lt;input type ="color"/> — 用js打开颜色选择器

JQuery 和复选框

linux - 忽略下一个字段与上一个字段相同的行