当前位置:主页 > 软件编程 > Python代码 >

在python 中实现运行多条shell命令

时间:2021-10-23 11:30:01 | 栏目:Python代码 | 点击:

使用py时可能需要连续运行多条shell 命令

1.

# coding: UTF-8
import sys
reload(sys)
sys.setdefaultencoding('utf8')

import subprocess
import os
import commands

#os.system('cmd1 && cmd2')
cmd1 = "cd ../"
cmd2 = "ls"
cmd = cmd1 + " && " + cmd2

#如下两种都可以运行
subprocess.Popen(cmd, shell=True)
subprocess.call(cmd,shell=True)

2.也可以将多条shell写入.sh 文件.然后运行.sh文件

您可能感兴趣的文章:

相关文章