Min*_*any 1 python shell command
在shell中,如果我只想在第一个命令成功时执行第二个命令,我会这样做
cmd1 && cmd2
Run Code Online (Sandbox Code Playgroud)
如果我需要执行第二个命令,如果第一个命令失败,我会这样做
cmd1 || cmd2
Run Code Online (Sandbox Code Playgroud)
我在python脚本中通过subprocess.call调用命令列表.我怎么能做到这一点?
传递shell=True给subprocess.call你,你可以执行任意shell命令.只要确保你正确地逃避/引用一切.
subprocess.call("true && echo yes!", shell=True)
Run Code Online (Sandbox Code Playgroud)
打印yes!,而
subprocess.call("false && echo yes!", shell=True)
Run Code Online (Sandbox Code Playgroud)
没有打印.
(你不需要转义或引用&&或者||,但是带有空格的文件名可能很痛苦.)