[問題] 用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
推
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
03/29 02:12, 2F
→
03/29 02:21, , 3F
03/29 02:21, 3F
→
03/29 02:21, , 4F
03/29 02:21, 4F
推
03/29 03:42, , 5F
03/29 03:42, 5F
→
03/29 11:31, , 6F
03/29 11:31, 6F
推
03/29 20:18, , 7F
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)