首页 > 编程 > Python > 正文

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

2019-11-25 13:32:00
字体:
来源:转载
供稿:网友

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

1.

# coding: UTF-8import sysreload(sys)sys.setdefaultencoding('utf8')import subprocessimport osimport 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文件

以上这篇在python 中实现运行多条shell命令就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持武林网。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表