2017-08-10 28 views
0

我的python程序的一部分使用子进程打开一个vbs脚本。Python 3.6尝试使用来自其他程序的命令

path = os.sep.join(['C:','Users',getpass.getuser(),'Desktop','Program','build','exe.win32-3.6','vbs.vbs']) 

subprocess.call([sys.executable, path]) 

但不是执行我的vbs脚本,而是尝试运行它作为python代码,并给我:NameError:msgbox未定义。 而当我手动运行VBS脚本它的作品。

我想让python正常执行vbs脚本。不要将它作为python代码运行。

+0

那么究竟什么是你的问题? –

+0

'sys.executable'是Python解释器。改为使用您想运行'.vbs'文件的可执行文件的名称。 – mkrieger1

+0

[使用由python创建的参数执行vbs文件]的可能重复(https://stackoverflow.com/questions/19112944/executing-a-vbs-file-with-arguments-created-by-python) – mkrieger1

回答

0

sys.executable指向系统的Python可执行程序。在你的情况下,可能类似C:\Python27\python.exe。你应该打印出来,看看自己。

要执行VBScript,您需要使用C:\Windows\system32\wscript.exe

另外,使用os.path.join()os.sep.join()更适合于该任务。

所以,你会结束:

system32 = os.path.join(os.environ['SystemRoot'], 'system32') 
wscript = os.path.join(system32, 'wscript.exe') 
path = os.sep.join(['C:','Users',getpass.getuser(),'Desktop','Program','build','exe.win32-3.6','vbs.vbs']) 
subprocess.call([wscript, path]) 
0

这是正是你告诉子过程做。从docs

sys.executable

A string giving the absolute path of the executable binary for the Python interpreter, on systems where this makes sense. If Python is unable to retrieve the real path to its executable, sys.executable will be an empty string or None .

相关问题