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

8
  • What do you mean by " I need these output files for further calculations."? What are you trying to do? Commented Feb 13, 2015 at 13:51
  • @Sandhyaa S: See if this post helps stackoverflow.com/questions/7604621/… Commented Feb 13, 2015 at 13:52
  • @LutzHorn: I need the output so that I can process them using another program Commented Feb 13, 2015 at 13:54
  • @MudassirRazvi: Will check the thread and let you know. Thank you. Commented Feb 13, 2015 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?? Commented Feb 13, 2015 at 14:00

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Ok. os.listdir() is something new to me. Will try it out. Thank you
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().

2 Comments

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.
@SandhyaaS Yes, try using Popen, if it doesn' t work, let's see what happens then.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.