I'm writing a Python program to call a local binary with Popen to capture its output. I change directories to it with os.chdir
and I have verified the file is there. However, the following code raises a 'file not found' exception.
Can anyone tell me what I'm doing wrong? Is there something special I have to do with running programs from directories not in my $PATH? Thanks in advance.
try:
os.chdir('/home/me')
p = sub.Popen(['./exec', '--arg', 'arg1'], cwd=os.getcwd(), stdout=sub.PIPE)
out, err = p.communicate()
print("done")
except OSError as e:
print("error %s" % e.strerror)
Note this section of the docs (emphasis mine):
If cwd is not None, the child’s current directory will be changed to cwd before it is executed. Note that this directory is not considered when searching the executable, so you can’t specify the program’s path relative to cwd.
Try using an absolute path with Popen
.
p = sub.Popen(['/home/me/exec', '--arg', 'arg1'], stdout=sub.PIPE)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With