Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.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中_Python_Shell_Command - Fatal编程技术网

壳牌公司';标准普尔&;和| |在python中

壳牌公司';标准普尔&;和| |在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

在shell中,如果我只想在第一个命令成功的情况下执行第二个命令,我会这样做

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
以引发
调用的进程错误。这里有一些有用的建议