您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

在Python中顺序执行命令?

在Python中顺序执行命令?

有一种简单的方法可以执行一系列命令。

在以下使用 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()

这将允许您构建一系列命令。

python 2022/1/1 18:25:14 有181人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶