我想创建一个 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/