2014-02-22 60 views
1

我正在使用Python pywin32運行應用程序。問題是我需要在應用程序exe文件的根目錄下才能成功運行它。讓我們舉個例子,我們想在應用程序根目錄下運行notepad ++。exe。在CMD我想這和它的工作:Python pywin32在其目錄中運行應用程序

C:\>(cd "C:\Program Files (x86)\Notepad++" && notepad++.exe) 

但是,如果與shell.Run運行它在Python:解碼時

Traceback (most recent call last): 
File "<stdin>", line 1, in <module> 
File "<COMObject WScript.Shell>", line 2, in Run 
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2147024894), None) 

其中:

import win32com.client 
shell = win32com.client.Dispatch("WScript.Shell") 
shell.Run('(cd "C:\Program Files (x86)\Notepad++" && notepad++.exe)') 

返回例外

import win32api 
e_msg = win32api.FormatMessage(-2147024894) 

奇怪地說:

'The system cannot find the file specified.\r\n' 

回答

1

它爲我的作品:

shell.Run('cmd /K (cd "C:\Program Files (x86)\Notepad++" && notepad++.exe)') 

,或者甚至更簡單:

shell.Run('cmd /K cd "C:\Program Files (x86)\Notepad++" && notepad++.exe') 
相關問題