简体   繁体   中英

How to execute an .EXE file in Python with a given filepath?

I'm trying to write a bit of code to allow me to open up an exe file within Python, but I don't know the general way to make it properly. I would think that the code would look something like this:

exec(open("C:\\Users\\user\\AppData\\Local\\Programs\\file-folder\\file.exe").read())

but when I compile the code using Geany, it gives me an error saying UnicodeDecodeError: 'charmap' codec can't decode byte 0x90 in position 2: character maps to undefined

I get the same error message when I try to write the file path with just one \ between each folder, but it gives me the same error in the compiler. Any help on this is greatly appreciated.

you can use subprocess.call :

import subprocess
subprocess.call(["fullPath\\yourExe.exe"])

well you need to import os in your code if you wanna execute external files

import os
os.startfile(""C:\\Users\\user\\AppData\\Local\\Programs\\file-folder\\file.exe"")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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