有一种简单的方法可以执行一系列命令。
在以下使用 subprocess.Popen
"command1; command2; command3"
或者,如果您陷在Windows中,则有多种选择。
创建一个临时的“ .BAT”文件,并将其提供给 subprocess.Popen
在单个长字符串中创建带有“ \ n”分隔符的命令序列。
像这样使用“”。
"""
command1
command2
command3
"""
或者,如果您必须零碎地做某事,则必须做这样的事情。
class Command( object ):
def __init__( self, text ):
self.text = text
def execute( self ):
self.proc= subprocess.Popen( ... self.text ... )
self.proc.wait()
class CommandSequence( Command ):
def __init__( self, *steps ):
self.steps = steps
def execute( self ):
for s in self.steps:
s.execute()
这将允许您构建一系列命令。