作者Holocaust123 (Terry)
看板Python
标题[问题] 用python执行接参数的执行档
时间Mon Mar 29 01:19:09 2010
(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
1F:推 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
2F:推 qwerasdft:"Program Files" 这个呢? 03/29 02:12
3F:→ Holocaust123:param = r'C:\"Program Files"\hfs\hfs.vfs'这样的话 03/29 02:21
4F:→ Holocaust123:产生一样的错误 03/29 02:21
5F:推 ibmibmibm:param要是list吧:['param1','param2','param3']之类的 03/29 03:42
6F:→ Holocaust123:spawnv(mode,path,args)的args才是list/tuple的样子. 03/29 11:31
7F:推 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)