2014-01-25 29 views
0

我正在嘗試創建一個程序,該程序將啓動帶有標誌(-example)的livestreamer.exe,但無法弄清楚如何執行此操作。如何在python中啓動程序時使用標誌/啓動參數?

在使用內置在Windows的「運行」功能,我輸入: livestreamer.exe twitch.tv/streamer best

這裏是到目前爲止我Python代碼:

import os 

streamer=input("Streamer (full name): ") 
quality=input("""Quality: 
Best 
High 
Medium 
Low 
Mobile 
: """).lower() 
os.chdir("C:\Program Files (x86)\Livestreamer") 
os.startfile("livestreamer.exe twitch.tv "+streamer+" "+quality) 

我明白的代碼是尋找一個沒有命名爲livestreamer.exe(FileNotFoundError)的文件,但其中包含所有其他代碼的文件。有誰知道如何使用內置的參數啓動程序?謝謝。

回答

0

使用os.system()代替os.file()。沒有名爲"livestreamer.exe twitch.tv "+streamer+" "+quality

此外os.system()氣餒文件,使用subprocess.Popen()來代替。 Subprocess.Popen()語法看起來更加複雜,但它更好,因爲一旦您知道subprocess.Popen(),就不需要其他任何東西。 subprocess.Popen()取代了其他幾個工具(os.system()就是其中之一),這些工具分散在其他三個Python模塊中。

如果有幫助,請將subprocess.Popen()視爲非常靈活的os.system()一個例子

sts = os.system("mycmd" + " myarg") 

做同樣具有:

sts = Popen("mycmd" + " myarg", shell=True).wait() 

OR

sts = Popen("mycmd" + " myarg", shell=True) 
sts.wait() 

來源:Subprocess.Popen and os.system()

+0

謝謝,它使用你的os.system(「livestreamer.exe twitch.tv/"+streamer+」「+ quality」)。現在我將使用Popen函數,看看我能否實現它。 .wait的東西有用嗎?是否需要包含? – jdiamond858

+0

wait()等待Popen進程完成,請閱讀http://docs.python.org/release/2.6.5/library/subprocess.html – Azwr

+0

我看到了,比如說如果我輸入了一個不流式傳輸的流並且命令文件沒有打開,那麼我在.wait函數後輸入的任何代碼都會立即運行嗎?我認爲這樣會很好爲了在抽搐中有多個頻道的廣泛的網絡實現 – jdiamond858

0

subprocess.call怎麼樣?喜歡的東西:

import subprocess 
subprocess.call(["livestreamer.exe", "streamer", "best"]) 
+0

感謝您的評論,howeve r我不確定如何讓這個工作,而Azwr的上面直接工作,並隱藏在後臺打開的cmd.exe。 – jdiamond858

相關問題