Running binary with "./" from subprocess.run

Viewed 575

I'm having some trouble running some binaries with subprocess.run

I have a binary file at /tools, lets call the binary program. So I need to call /tools/program.

Every option I try, subprocess shows that the file does not exist. I have tried the following.

ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
TOOL_DIR = ROOT_DIR + '/tools/program'

# All of the following return error not found
program_subprocess = subprocess.run(['./'+TOOL_DIR])
program_subprocess = subprocess.run(['./"'+TOOL_DIR+'"'])
# Tried without the /, cause TOOL_DIR has a slash at the start
program_subprocess = subprocess.run(['.'+TOOL_DIR])

Any idea on how to run binaries with subprocess.run using ./?

EDIT: Also tried

subprocess.run([./\"+TOOL_DIR+'\"'])

1 Answers

When running subprocess the operating system has to find the executable file somehow. Normally, executables are installed, can be found in PATH and you just use the program name - subprocess.run(["program"]).

In your case it looks like you know the absolute path to the executable, so you can use it - subprocess.run(["/tools/program"]). The downside is that your script can only be run on machines that have the binary in the /tools directory.

Related