将参数传递给 subprocess.Popen() 调用的“可执行”参数

Sha*_*odi 4 python subprocess tcsh

subprocess.Popen() 允许您通过“可执行”参数传递您选择的 shell。
我选择传递“/bin/tcsh”,并且我不希望 tcsh 读取我的~/.cshrc.
tcsh 手册说我需要传递-fto/bin/tcsh才能做到这一点。

如何让 Popen 使用 -f 选项执行 /bin/tcsh?

import subprocess

cmd = ["echo hi"]
print cmd

proc = subprocess.Popen(cmd, shell=False,  executable="/bin/tcsh", stderr=subprocess.PIPE, stdout=subprocess.PIPE)
return_code = proc.wait()

for line in proc.stdout:
    print("stdout: " + line.rstrip())

for line in proc.stderr:
    print("stderr: " + line.rstrip())

print return_code
Run Code Online (Sandbox Code Playgroud)

Mic*_*ild 5

让您的生活更轻松:

subprocess.Popen(['/bin/tcsh', '-f', '-c', 'echo hi'],
    shell=False, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
Run Code Online (Sandbox Code Playgroud)