[問題] 用python執行接參數的執行檔

看板Python作者 (Terry)時間15年前 (2010/03/28 17:19), 編輯推噓4(403)
留言7則, 5人參與, 最新討論串1/1
(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

03/29 01:30, , 1F
加個引號?
03/29 01:30, 1F
請問你是說這樣嗎: 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

03/29 02:12, , 2F
"Program Files" 這個呢?
03/29 02:12, 2F

03/29 02:21, , 3F
param = r'C:\"Program Files"\hfs\hfs.vfs'這樣的話
03/29 02:21, 3F

03/29 02:21, , 4F
產生一樣的錯誤
03/29 02:21, 4F

03/29 03:42, , 5F
param要是list吧:['param1','param2','param3']之類的
03/29 03:42, 5F

03/29 11:31, , 6F
spawnv(mode,path,args)的args才是list/tuple的樣子.
03/29 11:31, 6F

03/29 20:18, , 7F
用subprocess.Popen([exe, ])
03/29 20:18, 7F
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)
文章代碼(AID): #1Bhu-FBB (Python)