简体   繁体   中英

How to run exe file from python?

I try to run a exe (In the background) from a specific path in my local python project, with the os.system libary. I have managed to change folders like 'cd' Command, but i can't run the file.

This is for a python project running on Windows 64BIT, Python 3.5.3

the file.exe is at the "programs" dir.

import os
os.system("cd C:\Users\User\AppData\Windows\Start Menu\Programs")
subprocess.Popen("file.exe")

Error:

{OSError}[WinError 193] %1 is not a valid Win32 application

I saw other posts about this issue but i couldn't resolve it. Any ideas?

Problem is that the system command doesn't work. It "works", but in a separate subprocess that exits right away. Current directory doesn't propagate up to the calling process (also, as you're not checking the return code, the command wouldn't fail, even with a non-existing directory. Note that it happens here, as the directory name has spaces in it and isn't quoted...).

You would have to use os.chdir for that, but you don't even need it.

If you want to run a command in a specific location, just pass the absolute path of the command (and since it's using string literals, always use r prefix to avoid that some \t or \n chars are interpreted as special characters...). For instance with python 3, with the command line provided there's an error (but okay in python 2...):

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 5-6: truncated \UXXXXXXXX escape

So always use raw prefix. Here's how I would rewrite that:

current_dir = r"C:\Users\User\AppData\Windows\Start Menu\Programs"
subprocess.Popen(os.path.join(current_dir,"file.exe"))

And if you really need that the current directory is the same as the exe, use cwd parameter. Also get the return value of Popen to be able to wait/poll/kill/whatever and get command exit code:

p = subprocess.Popen(os.path.join(current_dir,"file.exe"),cwd=current_dir)
# ...
return_code = p.wait()

As a side note, be aware that:

p = subprocess.Popen("file.exe",cwd=current_dir)

doesn't work, even if file.exe is in current_dir (unless you set shell=True , but it's better to avoid that too for security/portability reasons)

Note that os.system is deprecated for a lot of (good) reasons. Use subprocess module, always , and if there are argument, always with an argument list (not string), and avoid shell=True as much as possible.

Can you see if this works

   import os 

    # change the current directory 
    # to specified directory 
    os.chdir(r"C:\Users\User\AppData\Windows\Start Menu\Programs") 
    subprocess.Popen("file.exe")

Problem solved. Thanks everyone, the issue was administrative privileges.. Started pycharm as administrator. And like i said - i was able to see the file with os.listdir(), but when i try to run it, errors start to pop up. I think the main issue is os.system() inherit current privileges from python proccess.

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