[英]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.我正在尝试编写一些代码以允许我在 Python 中打开一个 exe 文件,但我不知道正确制作它的一般方法。 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但是当我使用 Geany 编译代码时,它给了我一个错误说 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
:您可以使用
subprocess.call
:
import subprocess
subprocess.call(["fullPath\\yourExe.exe"])
well you need to import os in your code if you wanna execute external files如果你想执行外部文件,你需要在你的代码中导入 os
import os
os.startfile(""C:\\Users\\user\\AppData\\Local\\Programs\\file-folder\\file.exe"")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.