简体   繁体   English

从python运行外部EXE

[英]Running external EXE from python

I'm running several portable apps from my python app. 我正在从python应用程序运行几个便携式应用程序。

Consider the following code: 考虑以下代码:

import win32com.shell.shell as w32shell
import os
import sys
import win32process as process

PORTABLE_APP_LOCATION = "C:\\Windows\\System32\\calc.exe"

#This function runs a portable application:
def runPortable():
    try:
        startObj = process.STARTUPINFO()
        process.CreateProcess(PORTABLE_APP_LOCATION,None,None,None,8,8,None,None,startObj)
        # OR
        #w32shell.ShellExecuteEx(lpFile=PORTABLE_APP_LOCATION)
    except:
        print(sys.exc_info()[0])
runPortable()

1) Should I expect any differences in the execution of this code from pythonw or python ? 1)我是否应该期望从pythonw或python的代码执行方面有任何不同?

2) If I change PORTABLE_APP_LOCATION to the path to a portable version of CDBurnerXP and use the ShellExecuteEx option instead of CreateProcess, I see the process is started on Windows Task Manager but the actual window of the app isn't visible. 2)如果将PORTABLE_APP_LOCATION更改为CDBurnerXP便携式版本的路径,并使用ShellExecuteEx选项而不是CreateProcess,我会看到该进程已在Windows Task Manager上启动,但该应用程序的实际窗口不可见。 This doesn't happen with other EXEs such as a portable version of GIMP that do show up after being ran. 对于其他EXE(例如GIMP的便携式版本),在运行后确实会出现这种情况。 I assume this difference comes from a property of the executables. 我认为这种差异来自可执行文件的属性。 Anybody knows what's causing this? 有人知道是什么原因造成的吗?

3) Under what terms does Windows prompts the "Are you sure you want to run this EXE"? 3)Windows在什么条件下提示“您确定要运行此EXE”? I believe CDBurnerXP is signed with a certificate but still sometimes Windows pops this question when trying to run this EXE from within python. 我相信CDBurnerXP已使用证书签名,但是当尝试从python内部运行此EXE时,Windows仍然有时会弹出此问题。

Thanks a lot. 非常感谢。

关于第一个问题,您应注意,使用pythonw.exe运行时执行python代码时,您的sys.stdout缓冲区限制为4096字节,并且在溢出时将抛出IOError,因为代码正在无窗口运行,因此您将看不到。

I am a newbie in this field. 我是这个领域的新手。 May be this can help you 也许这可以帮助您

use subprocess.call , more info here : 使用subprocess.call ,更多信息在这里

import subprocess
subprocess.call(["C:\\temp\\calc.exe"])

or 要么

import os
os.system('"C:/Windows/System32/notepad.exe"')

i hope it helps you... 我希望它可以帮助您...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM