Python 2.7 下面的代码如何将os.system()调用更改为SUBSPROCESS.call()

Python 2.7 下面的代码如何将os.system()调用更改为SUBSPROCESS.call(),python-2.7,Python 2.7,需要知道如何使用shell=False将下面的代码从os.system转换为subprocess.call 要修改的代码: command1="ls -lrt" command2="cat file.txt" cucDBServiceStartRC = os.WEXITSTATUS(os.system(command1 + " && " + command2)) if(cucDBServiceStartRC!=0); do something.. 我试过: comma

需要知道如何使用shell=False将下面的代码从os.system转换为subprocess.call

要修改的代码:

command1="ls -lrt"
command2="cat file.txt"
cucDBServiceStartRC =  os.WEXITSTATUS(os.system(command1 + " && " + command2))
if(cucDBServiceStartRC!=0);
    do something..
我试过:

command1="ls -lrt"
command2="cat file.txt"
cucDBServiceStartRC = os.WEXITSTATUS(subprocess.call(shlex.split(command1 + " && " + command2),shell=False))
if(cucDBServiceStartRC!=0);
    do something..
但命令无法编译


注意:我想使用
shell=False
,因此我需要在子流程中使用&&(在上面的
os.system
代码中使用)的变通方法,以便一次运行两个命令。

&
是一个shell操作符。如果要运行两个程序而中间没有shell,则需要运行
子进程。调用
两次:

returnCode = subprocess.call(shlex.split(command1), shell=False);
if returnCode == 0:
    returnCode = subprocess.call(shlex.split(command2), shell=False);
// Do whatever with returnCode. A value of 0 means either command failed.
最好不要使用
shlex.split
分割命令行。相反,从一开始就将可执行文件名与参数分开。否则,如果用户可以影响
command1
command2
的内容,则您将面临与
shell=True
相同的安全问题