壳牌公司';标准普尔&;和| |在python中
在shell中,如果我只想在第一个命令成功的情况下执行第二个命令,我会这样做壳牌公司';标准普尔&;和| |在python中,python,shell,command,Python,Shell,Command,在shell中,如果我只想在第一个命令成功的情况下执行第二个命令,我会这样做 cmd1 && cmd2 cmd1 || cmd2 如果我需要执行第二个命令,如果第一个命令失败,我会这样做 cmd1 && cmd2 cmd1 || cmd2 我通过python脚本中的subprocess.call调用命令列表。如何执行上述操作?将shell=True传递给子流程。调用,您可以执行任意shell命令。只要你确定 打印是,而 subprocess.call("f
cmd1 && cmd2
cmd1 || cmd2
如果我需要执行第二个命令,如果第一个命令失败,我会这样做
cmd1 && cmd2
cmd1 || cmd2
我通过python脚本中的subprocess.call调用命令列表。如何执行上述操作?将
shell=True
传递给子流程。调用,您可以执行任意shell命令。只要你确定
打印是代码>,而
subprocess.call("false && echo yes!", shell=True)
不打印任何内容
(您不需要转义或引用&&&
或|
,但文件名中带有空格可能会很麻烦。)将shell=True
传递到子进程。调用,您可以执行任意shell命令。只要你确定
打印是代码>,而
subprocess.call("false && echo yes!", shell=True)
不打印任何内容
(您不需要转义或引用&&&&
或| |
,但文件名中带有空格可能会很麻烦。)这种类型的东西可以工作
In [227]: subprocess.Popen(['dir', '||', 'ls'], shell=True)
Out[227]: <subprocess.Popen at 0x1e4dfb0>
Volume in drive D has no label.
Volume Serial Number is 4D5A-03B6
Directory of D:\Dummy
09/14/2012 03:54 PM <DIR> .
09/14/2012 03:54 PM <DIR> ..
0 File(s) 0 bytes
2 Dir(s) 141,482,524,672 bytes free
In [228]: subprocess.Popen(['dir', '&&', 'ls'], shell=True)
Out[228]: <subprocess.Popen at 0x1e4df10>
Volume in drive D has no label.
Volume Serial Number is 4D5A-03B6
Directory of D:\Dummy
09/14/2012 03:54 PM <DIR> .
09/14/2012 03:54 PM <DIR> ..
0 File(s) 0 bytes
2 Dir(s) 141,482,524,672 bytes free
'ls' is not recognized as an internal or external command,
operable program or batch file.
[227]中的:subprocess.Popen(['dir','| |','ls',shell=True)
出[227]:
驱动器D中的卷没有标签。
卷序列号为4D5A-03B6
D:\Dummy的目录
2012年9月14日下午3:54。
2012年9月14日下午3:54。。
0个文件0个字节
2个目录141482524672字节可用
在[228]中:subprocess.Popen(['dir','&&&','ls'],shell=True)
出[228]:
驱动器D中的卷没有标签。
卷序列号为4D5A-03B6
D:\Dummy的目录
2012年9月14日下午3:54。
2012年9月14日下午3:54。。
0个文件0个字节
2个目录141482524672字节可用
“ls”未被识别为内部或外部命令,
可操作的程序或批处理文件。
这样的东西会有用的
In [227]: subprocess.Popen(['dir', '||', 'ls'], shell=True)
Out[227]: <subprocess.Popen at 0x1e4dfb0>
Volume in drive D has no label.
Volume Serial Number is 4D5A-03B6
Directory of D:\Dummy
09/14/2012 03:54 PM <DIR> .
09/14/2012 03:54 PM <DIR> ..
0 File(s) 0 bytes
2 Dir(s) 141,482,524,672 bytes free
In [228]: subprocess.Popen(['dir', '&&', 'ls'], shell=True)
Out[228]: <subprocess.Popen at 0x1e4df10>
Volume in drive D has no label.
Volume Serial Number is 4D5A-03B6
Directory of D:\Dummy
09/14/2012 03:54 PM <DIR> .
09/14/2012 03:54 PM <DIR> ..
0 File(s) 0 bytes
2 Dir(s) 141,482,524,672 bytes free
'ls' is not recognized as an internal or external command,
operable program or batch file.
[227]中的:subprocess.Popen(['dir','| |','ls',shell=True)
出[227]:
驱动器D中的卷没有标签。
卷序列号为4D5A-03B6
D:\Dummy的目录
2012年9月14日下午3:54。
2012年9月14日下午3:54。。
0个文件0个字节
2个目录141482524672字节可用
在[228]中:subprocess.Popen(['dir','&&&','ls'],shell=True)
出[228]:
驱动器D中的卷没有标签。
卷序列号为4D5A-03B6
D:\Dummy的目录
2012年9月14日下午3:54。
2012年9月14日下午3:54。。
0个文件0个字节
2个目录141482524672字节可用
“ls”未被识别为内部或外部命令,
可操作的程序或批处理文件。
在@larsmans answer上展开-如果您正在使用子流程。调用并且不想设置shell=True
,无论出于何种原因,您都可以检查returncode
属性。
如果返回代码为0,则命令成功。您还可以实现一个子进程。选中\u call
以引发调用的进程错误。这里有一些有用的扩展@larsmans的答案-如果您正在使用子流程。调用
并且不想设置shell=True
,无论出于何种原因,您都可以检查返回代码
属性。
如果返回代码为0,则命令成功。您还可以实现一个子进程。选中\u call
以引发调用的进程错误。这里有一些有用的建议