2014-11-04 128 views
1

我對Python仍然很陌生,現在仍然對自己能做的所有事情都很滿意。我目前正在製作一個小應用程序來幫助我工作。它有一個圖形菜單,可以讓我選擇在PC上安裝的應用程序。我遇到的問題是一些按鈕有兩個安裝程序(32和64位)。我需要程序在開始下一個之前等待一個完成。我該怎麼做呢?如何在Python中一個接一個地運行多個可執行文件

下面的代碼...

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") 

下面是編輯的代碼後,我從史蒂夫的變化......也是錯誤的消息,我得到的蟒蛇終端運行安裝程序(這似乎工作崗位)。

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' 

謝謝!

回答

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阻塞。這意味着它會等待您啓動完成的任何操作,並且在安裝程序關閉時腳本將立即啓動。

+0

感謝您的答覆。當我嘗試用subprocess.call啓動一個exe文件時,我得到了這個 - 「操作系統錯誤:[WinError 740]請求的操作需要提升」 我假設這意味着他們需要管理員權限?我是我個人電腦上的管理員,通常沒有問題。 – sloppyfrenzy 2014-11-04 17:37:04

+0

@sloppyfrenzy奇怪。當我嘗試安裝程序時,安裝程​​序正常啓動,要求用戶提升權限。你可以以管理員身份啓動你的python腳本,但這不是很優雅。 – 2014-11-04 17:44:07

+0

奇怪。我正在運行Win 8。1如果這有什麼區別。當我右鍵單擊python文件時,我沒有看到以管理員身份運行的選項。子進程比一般的操作系統更好用嗎? – sloppyfrenzy 2014-11-04 18:15:28

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" 
+0

感謝您的回覆。我不確定那些代碼是否誠實,至少與我遇到的問題有關。我會在哪裏把它放在我的代碼中?感謝您添加「r」提示。 – sloppyfrenzy 2014-11-04 18:25:24

+0

@sloppyfrenzy我已經將示例擴展爲一個可以使用和評論它的函數。 – 2014-11-04 18:33:04

+0

感謝您的解釋。我仍然不清楚一些事情。 「print >> sys.stderr」具體做什麼?此外,我想出瞭如何讓它工作,但我不知道如何使用此代碼的多個文件路徑。目前我在「mycmd」變量中有文件路徑。我使用我嘗試過的更新後的代碼編輯了我的帖子,並且在運行其中一個安裝程序後,我在python終端中收到了錯誤消息。安裝程序運行,但然後我得到錯誤。 – sloppyfrenzy 2014-11-05 18:42:40

相關問題