2

I am trying to write a Python script to run recursively a program from bash in multiple directories and save(now just display) output in log file.

The problem is that when I try to run that app from home directory just giving the full path of input files it crashes, so I usually run it directly in directory containing input files. I have tried to do this with os.popen() and pexpect but I think most suitable here will be subprocess.

My code:

import subprocess,
from subprocess import Popen, PIPE 

subprocess.Popen("$fp "+pcr+" "+dat, cwd=path+dirs[0], stdout=PIPE)
print output.stdout.read()

where pcr and dat are the names of input files.

As I try to run it I obtain following error:

Traceback (most recent call last):
File "file_ed.py", line 47, in <module>
subprocess.Popen("$fp "+pcr+" "+dat, cwd=retval, stdout=PIPE)
File "/usr/lib/python2.7/subprocess.py", line 710, in __init__
errread, errwrite)
 File "/usr/lib/python2.7/subprocess.py", line 1335, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory

What could be the cause of this problem and how to fix it?

Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
Silneus
  • 21
  • 3
  • 1
    You are trying to access a file or run a command that doesn't exist. There might not be a command `$fp`, for instance. See [this question](http://stackoverflow.com/questions/26643719/python-exporting-environment-variables-in-subprocess-popen) for info about using environmental variables with subprocess. – Håken Lid Aug 08 '15 at 14:12
  • The best way to run a series of bash commands is by writing a bash script to do it. Your approach is doing it the hard way. – msw Aug 08 '15 at 14:22

1 Answers1

2

I think your problem is that you want to pass pcr and dat as arguments to the program and you think that the whitespace will do that for you – but Python is not bash. Just use an array and it should work:

subprocess.Popen([fp, pcr, dat], cwd=path+dirs[0], stdout=PIPE)

The reason you got "no such file or directory": Python was looking for a program named "$fp "+pcr+" "+dat, i.e. it assumed that the whitespace (and the dollar sign, unless you meant it as kind of a pseudo-code placeholder) is part of the program name. The error tells you that there is no program with such a name.

However it is hard to guess what you mean because your snippet is incomplete. I don't really know what $fp and pcr and dat mean.

Also have a look at the examples in the documentation of the subprocess module.

betterworld
  • 234
  • 1
  • 5
  • Sorry for this ambiguous `$fp dat` and `pcr`. the last two are as I said strings containing input files for the app I want to run. `$fp` is symbolic link to the app in bash, but I don't think it will work that way so I changed it to the full path. I have solved it using pexpect: `child = pexpect.run('/usr/local/bin/FullProf_Suite/fp2k '+pcr+' '+dat,logfile=sys.stdout,cwd=path+dirs[0])` – Silneus Aug 09 '15 at 15:41
  • where the' path' and 'dirs[0]' are just string forming the path to my work directory. Thanks for reply! – Silneus Aug 09 '15 at 15:47