Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.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 从os.system切换到Popen生成交互式shell的正确方法是什么?_Python_Shell - Fatal编程技术网

Python 从os.system切换到Popen生成交互式shell的正确方法是什么?

Python 从os.system切换到Popen生成交互式shell的正确方法是什么?,python,shell,Python,Shell,我试图在我的python代码中去掉os.system,大部分都完成了,但我有一行代码似乎无法转换 os.system('/bin/sh -i -c "/bin/runner catch exts"') 到目前为止,尝试的方法是: p = subprocess.Popen(['/bin/sh', '-i', '-c', '/bin/runner catch exts']) 这将返回: /bin/sh: 1: Cannot set tty process group (No such proce

我试图在我的python代码中去掉os.system,大部分都完成了,但我有一行代码似乎无法转换

os.system('/bin/sh -i -c "/bin/runner catch exts"')
到目前为止,尝试的方法是:

p = subprocess.Popen(['/bin/sh', '-i', '-c', '/bin/runner catch exts'])
这将返回:

/bin/sh: 1: Cannot set tty process group (No such process)
编辑:

我没有运行我的自定义runner,而是用“ls”尝试了这一点,但仍然得到相同的错误

os.system('/bin/sh -i -c "ls"') # works fine
现在正在尝试转换--

返回:

/bin/sh: 1: Cannot set tty process group (No such process)
OSError: [Errno 2] No such file or directory
正在尝试(这不应该起作用,但用户在评论中建议):

返回:

/bin/sh: 1: Cannot set tty process group (No such process)
OSError: [Errno 2] No such file or directory

使用
Popen
无法使用交互式shell。
子进程
模块旨在让您完全控制子进程的stdin和stdout,以便与之对话


另一方面,交互式shell需要一个tty(终端),Python应该不使用它的I/O流。对于这个用例,您可以尝试使用
pty
模块,或者继续使用
os.system()

您应该使用subprocess.call()而不是subprocess.Popen()。Popen将子流程的stdin设置为管道,但sh-i希望将其stdin用作tty。

什么是
/bin/runner
?如何启动python脚本?自定义应用程序,python正在正常运行
python tryMe.py
无法复制。
/bin/runner
对shell/terminal/stdout做了什么特别的处理吗?
p=subprocess.Popen(“/bin/sh-i-c”/bin/runner catch exts”)
这行吗?您可以在子流程中尝试使用与os.system相同的参数,而不是使用参数。@DA14您可以使用
shell=True
并将命令行作为字符串传递,或者不指定
shell=True
并使用字符串列表(正如OP所做的那样)。使用带有
shell=False的字符串时,整个字符串将用作要执行的进程的名称,因此失败。