看板 Python 關於我們 聯絡資訊
(OS是XP,Python版本2.6.4) 我想用Python執行某個exe檔 該exe檔後面接一個參數 並希望Python跑完script後馬上關閉 而不要等到exe結束才關閉 script如下: import os exe = r'C:\Program Files\hfs\hfs.exe' param = r'C:\Program Files\hfs\hfs.vfs' os.spawnl(os.P_NOWAIT, exe, param) 執行該script後hfs.exe的確有被執行 但沒吃到hfs.vfs這個參數... (我直接把 hfs.vfs 拖曳到 hfs.exe 的圖示上是可以正常執行的) 請問我哪邊弄錯了? 謝謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.31.144
danqing:加個引號? 03/29 01:30
請問你是說這樣嗎: import os exe = r'"C:\Program Files\hfs\hfs.exe"' param = r'"C:\Program Files\hfs\hfs.vfs"' os.spawnl(os.P_NOWAIT, exe, param) 出現錯誤耶QQ Traceback (most recent call last): File "C:\Program Files\hfs\run.py", line 4, in <module> os.spawnl(os.P_NOWAIT, exe, param) File "C:\Program Files\Python26\lib\os.py", line 612, in spawnl return spawnv(mode, file, args) OSError: [Errno 22] Invalid argument
qwerasdft:"Program Files" 這個呢? 03/29 02:12
Holocaust123:param = r'C:\"Program Files"\hfs\hfs.vfs'這樣的話 03/29 02:21
Holocaust123:產生一樣的錯誤 03/29 02:21
ibmibmibm:param要是list吧:['param1','param2','param3']之類的 03/29 03:42
Holocaust123:spawnv(mode,path,args)的args才是list/tuple的樣子. 03/29 11:31
doghib:用subprocess.Popen([exe, ]) 03/29 20:18
thx 成功了 參考 Python v2.6.4 documentation 的 18.1.3.4 (Replacing the os.spawn family) P_NOWAIT example: pid = os.spawnlp(os.P_NOWAIT, "/bin/mycmd", "mycmd", "myarg") ==> pid = Popen(["/bin/mycmd", "myarg"]).pid ...(下略) import subprocess exe = r'C:\Program Files\hfs\hfs.exe' param = r'C:\Program Files\hfs\hfs.vfs' subprocess.Popen([exe, param]).pid ※ 編輯: Holocaust123 來自: 140.112.31.144 (03/29 20:44)