2017-08-10 25 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 .

相關問題