0

I am using Python 2.7.3 in Ubuntu 12.04 OS. I have an external program say 'xyz' whose input is a single file and two files say 'abc.dat' and 'gef.dat' are its output.

When I used os.system or subprocess.check_output or os.popen none of them printed the output files in the working directory. I need these output files for further calculations.

Plus I've to keep calling the 'xyz' program 'n' times and have to keep getting the output 'abc.dat' and 'gef.dat' every time from it. Please help. Thank you

aberna
  • 5,594
  • 2
  • 28
  • 33
  • What do you mean by " I need these output files for further calculations."? What are you trying to do? –  Feb 13 '15 at 13:51
  • @Sandhyaa S: See if this post helps http://stackoverflow.com/questions/7604621/call-external-program-from-python-and-get-its-output – Mudassir Razvi Feb 13 '15 at 13:52
  • @LutzHorn: I need the output so that I can process them using another program – Sandhyaa S Feb 13 '15 at 13:54
  • @MudassirRazvi: Will check the thread and let you know. Thank you. – Sandhyaa S Feb 13 '15 at 13:54
  • @MudassirRazvi: I went through the link and I found this - >>> subprocess.check_output(["ls", "-l", "/dev/null"]) 'crw-rw-rw- 1 root root 1, 3 Oct 18 2007 /dev/null\n' Can you please explain what this means?? – Sandhyaa S Feb 13 '15 at 14:00
  • [```ls```](http://linux.about.com/od/commands/l/blcmdl1_ls.htm) is a linux command that lists directory contents - the ```crw-rw-....``` is the result of the command's execution. – wwii Feb 13 '15 at 14:36
  • Where are the files being saved? – wwii Feb 13 '15 at 14:42
  • @wwii : The files are saved in the working directory. It is the program's output. – Sandhyaa S Feb 14 '15 at 04:52

3 Answers3

0

I can not comment on your question because my reputation is too low.

If you use os.system or subprocess.check_output or os.popen, you will just get the standard output of your xyz program (if it is printing something in the screen). To see the files in some directory, you can use os.listdir(). Then you can use these files in your script afterwards. It may also be worth using subprocess.check_call. There may be other better and more efficient solutions.

Abhishek
  • 279
  • 2
  • 5
  • 18
0

First, run the program which you invoked in python script directly and see if it generates those two files.

Assume it does, the problem is in your python script. Try using subprocess.Popen then call communicate().

Here's an example:

from subprocess import Popen

p = Popen(["xyz",])
p.communicate()

communicate waits for process to terminate. you should be able to get output files when executing code after p.communicate().

laike9m
  • 18,344
  • 20
  • 107
  • 140
  • The program xyz runs if I run it from the terminal and gives the output. However, when I try to execute it using python script the program runs but does not print the output files to the working directory. The problem must be with my script. I had used like this - import subprocess subprocess.check_output('/path/to/program/xyz abc.dat') where 'abc.dat' is my input. I shall use p.communicate(). Thanks a lot. – Sandhyaa S Feb 14 '15 at 05:01
  • @SandhyaaS Yes, try using `Popen`, if it doesn' t work, let's see what happens then. – laike9m Feb 14 '15 at 05:04
0

Thank you for answering my question but the answer to my question is this -

import subprocess subprocess.call("/path/to/software/xyz abc.dat", shell=True)

which gave me the desired the output.

I tried the subprocess-related commands but they returned error " No such file or directory". The 'shell=True' worked like a charm.

Thank you all again for taking your time to answer my question.