如何在python中依次运行多个可执行文件

时间:2014-11-04 17:27:21

标签: python

我仍然是Python的新手,我仍然围绕着它可以做的一切。我目前正在开发一个小应用程序来帮助我完成工作。它有一个图形菜单,可让我选择要在PC上安装的应用程序。我遇到的问题是一些按钮有两个安装程序(32位和64位)。我需要程序在开始下一个程序之前等待一个程序完成。我该怎么做?

这是代码......

def retranslateUi(self, Form):
    Form.setWindowTitle(_translate("Form", "Form", None))
    self.label.setText(_translate("Form", "What would you like to install?", None))
    self.adobe_reader.setText(_translate("Form", "Adobe Reader", None))
    self.flash.setText(_translate("Form", "Flash", None))
    self.java_7.setText(_translate("Form", "Java 7", None))
    self.java_8.setText(_translate("Form", "Java 8", None))
    self.adobe_reader.clicked.connect(self.adobe)
    self.flash.clicked.connect(self.flash13)
    self.java_7.clicked.connect(self.java7)
    self.java_8.clicked.connect(self.java8)

def adobe(self):
    os.startfile("C:\\Users\\Erik\\Desktop\\install_reader_11.exe")

def flash13(self):
    os.startfile("C:\\Users\\Erik\\Desktop\\install_flash_13_IE.exe")
    os.startfile("C:\\Users\\Erik\\Desktop\\install_flash_13_nonIE.exe")

def java7(self):
    os.startfile("C:\\Users\\Erik\\Desktop\\install_java-7u71-x32.exe")
    os.startfile("C:\\Users\\Erik\\Desktop\\install_java-7u71-x64.exe")

def java8(self):
    os.startfile("C:\\Users\\Erik\\Desktop\\install_java-8u25-x32.exe")
    os.startfile("C:\\Users\\Erik\\Desktop\\install_java-8u25-x64.exe")

这是我从Steve做出更改后编辑的代码...也是我在运行安装程序后看到python终端的错误消息(这似乎有用)。

def adobe(mycmd):

    mycmd = r"C:\Users\Erik\Desktop\install_reader_11.exe"

    try:
        retcode = call(mycmd,shell = True)
        if retcode < 0:
            print >>sys.stderr, "Child was terminated by signal", -retcode
        else:
            print >>sys.stderr, "Child returned", retcode

    except OSError as e:
        print >>sys.stderr, "Execution failed:", e
        retcode = -1

    return retcode

def flash13(mycmd):

    mycmd = r"C:\Users\Erik\Desktop\install_flash_13_IE.exe"

    try:
        retcode = call(mycmd,shell = True)
        if retcode < 0:
            print >>sys.stderr, "Child was terminated by signal", -retcode
        else:
            print >>sys.stderr, "Child returned", retcode

    except OSError as e:
        print >>sys.stderr, "Execution failed:", e
        retcode = -1

    return retcode

错误消息...

print >> sys.stderr, "Child returned", retcode
TypeError: unsupported operand type(s) for >>: 'builtin_function_or_method' and
'_io.TextIOWrapper'

谢谢!

2 个答案:

答案 0 :(得分:3)

您使用os.startfile(cmd)使用subprocess.call(cmd)的任何地方。在脚本的顶部放

import subprocess

并改变所有

的地方
os.startfile("C:\\Users\\Erik\\Desktop\\whatever.exe")

subprocess.call("C:\\Users\\Erik\\Desktop\\whatever.exe")

对脚本使用os.startfile的问题是它没有阻塞。这意味着您的脚本不会等待以便用户与弹出的安​​装程序进行交互;它只是继续前进,它可以立即弹出几个安装程序。 subprocess.call 阻止。这意味着等待您启动的任何内容完成,并且当安装程序关闭时,您的脚本将立即启动。

答案 1 :(得分:1)

而不是os.startfile使用类似下面的例子,我会把它放到一个函数中:

您需要在代码的早期:

from subprocess import call

def SafeExtern(mycmd):
    """ Wrapper to call external programs checking the results """
    try: # This allows exceptions to be caught
        retcode = call(mycmd, shell=True) # Call the external program
        if retcode < 0: # Check the return code errors should be <0
            print >>sys.stderr, "Child was terminated by signal", -retcode
        else:
            print >>sys.stderr, "Child returned", retcode # For information
    except OSError as e: # Catch OSErrors and let the user know
        print >>sys.stderr, "Execution failed:", e
        retcode = -1 # Obviously this is an error
    return retcode

然后,您可以使用上述功能调用外部程序,以便在每种情况下等待结果。

请注意,如果为每个字符串添加前缀r,则可以删除双反斜杠,例如:

r"C:\Users\Erik\Desktop\install_java-7u71-x32.exe"

而不是

"C:\\Users\\Erik\\Desktop\\install_java-7u71-x32.exe"