Python pywin32在其目录中运行应用程序

时间:2014-02-22 11:23:08

标签: python pywin32

我正在使用Python pywin32来运行应用程序。问题是我需要在应用程序exe文件的根目录下才能成功运行它。让我们举例说,我们想在应用程序根目录中运行notepad ++。exe。在CMD中我试过这个并且它有效:

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

但是如果在python中使用shell.Run运行它:

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

返回异常:

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 win32api
e_msg = win32api.FormatMessage(-2147024894)

奇怪地说:

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

1 个答案:

答案 0 :(得分: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')