Running exe files in python

how to run an exe file with the arguments using python

Suppose I have a file RegressionSystem.exe . I want to execute this executable with a -config argument. The commandline should be like:

RegressionSystem.exe -config filename 
regression_exe_path = os.path.join(get_path_for_regression,'Debug','RegressionSystem.exe') config = os.path.join(get_path_for_regression,'config.ini') subprocess.Popen(args=[regression_exe_path,'-config', config]) 

5 Answers 5

You can also use subprocess.call() if you want. For example,

import subprocess FNULL = open(os.devnull, 'w') #use this if you want to suppress output to stdout from the subprocess filename = "my_file.dat" args = "RegressionSystem.exe -config " + filename subprocess.call(args, stdout=FNULL, stderr=FNULL, shell=False) 

The difference between call and Popen is basically that call is blocking while Popen is not, with Popen providing more general functionality. Usually call is fine for most purposes, it is essentially a convenient form of Popen . You can read more at this question.

For anyone else finding this you can now use subprocess.run() . Here is an example:

import subprocess subprocess.run(["RegressionSystem.exe", "-config filename"]) 

The arguments can also be sent as a string instead, but you’ll need to set shell=True . The official documentation can be found here.

And again, the documentation is crap. Do we really need to pass «-config filename» or is it «-config», «filename» ? Calling the executable an argument does not really make sense either.

os.system("/path/to/exe/RegressionSystem.exe -config "+str(config)+" filename") 

os.system appears to block. That is, not return until the exe has run to completion. That is a noteworthy feature/constraint of this propose solution.

Читайте также:  Размыть задний фон css

Here i wanna offer a good example. In the following, I got the argument count of current program then append them in an array as argProgram = [] . Finally i called subprocess.call(argProgram) to pass them wholly and directly :

import subprocess import sys argProgram = [] if __name__ == "__main__": # Get arguments from input argCount = len(sys.argv) # Parse arguments for i in range(1, argCount): argProgram.append(sys.argv[i]) # Finally run the prepared command subprocess.call(argProgram) 

In this code i supposed to run an executable application named `Bit7z.exe» :

python Bit7zt.py Bit7zt.exe -e 1.zip -o extract_folder 

Notice : I used of for i in range(1, argCount): statement because i dont need the first argument.

Источник

How to run Exe from Python – 3 Easy methods?

Running an .exe file from Python can be a useful tool for automating certain tasks or for executing system commands.

including data analysis, machine learning, and web development. One of the many things that Python can do is run executable files (.exe) on a computer. This can be done using a few different methods, each with its own set of advantages and disadvantages. In this article, we will discuss how to run exe using Python.

In this article, we will explore how to run an .exe file from Python and discuss some of the benefits and limitations of this method.

1. Run exe with Python using Sub process

One way to run an .exe file from Python is to use the subprocess module. The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes.

This means that you can run an .exe file as a subprocess and interact with it just as you would with any other process.

For example, to run an .exe file named “example.exe” from Python, you can use the following code:

import subprocess subprocess.run(["example.exe"])

This code imports the subprocess module and uses the run() function to execute the “example.exe” file. The run() function takes a list of arguments, with the first argument being the name of the .exe file to run.

2. Run using OS Python Library

Another way to run an .exe file from Python is to use the os module. The os module provides a way of using operating system dependent functionality like reading or writing to files, starting new processes, killing processes etc.

To run an .exe file, you can use the os.system() function.

For example, to run an .exe file named “example.exe” from Python, you can use the following code:

import os os.system("path/to/example.exe")

This code imports the os module and uses the system() function to execute the “example.exe” file. The system() function takes a string argument, which is the command to be executed by the operating system shell.

Running an .exe file from Python can be a powerful tool for automating tasks, such as running system commands or launching other programs. However, it’s important to note that executing an .exe file can also be a security risk, as it can potentially allow malicious code to run on your system. Therefore, it is important to make sure that the .exe file you are running is from a trusted source before executing it.

3. Run Exe from Python using Win32Api

A third method is to use the “win32api” module, which is a python wrapper for Windows API. This module provides a way to access the Windows API, which can be used to run exe files. Here is an example of how to use the “win32api” module to run an exe file:

import win32api win32api.ShellExecute(0, "open", "path/to/exe", None, ".", 0)

This will run the exe file located at “path/to/exe” on your computer.

In conclusion, Python provides several ways to run exe files, each with its own set of advantages and disadvantages. The subprocess module is a powerful and flexible option that allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. The os module provides a way of using operating system dependent functionality like reading or writing to files, starting new processes, killing processes etc. The “win32api” module is a python wrapper for Windows API which can be used to run exe files.

It is important to note that running exe files using python may require certain permissions, and it is important to ensure that the exe file is from a trusted source before running it. Always use proper caution and security measures when running exe files using python or any other method.

In conclusion, running an .exe file from Python can be a useful tool for automating certain tasks or for executing system commands. By using the subprocess or os module, you can run an .exe file as a subprocess and interact with it just as you would with any other process. However, it’s important to be aware of the security risks involved and to make sure that the .exe file you are running is from a trusted source before executing it.

Источник

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") 

I might be wrong, but I think changing directory with an os.system call only changes the directory for that call. A shell is opened, the command is executed, and the shell is closed. Use os.chdir instead to change the scripts working directory.

@Moshe, very strange. That is not how it acts on my machine, even if I run python as admin os.getcwd(); os.system(«cd Desktop»); os.getcwd() will display the same directory and no change will happen. This happens regardless of me being in an interactive python shell, or running a .py file.

3 Answers 3

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.

Источник

Оцените статью