脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - 在python 中实现运行多条shell命令

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

2021-05-12 00:22xiojing825 Python

今天小编就为大家分享一篇在python 中实现运行多条shell命令,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

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

1.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 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文件

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

原文链接:https://blog.csdn.net/xiojing825/article/details/78207862

延伸 · 阅读

精彩推荐