2

I have written a C code where I have converted one file format to another file format. To run my C code, I have taken one command line argument : filestem.

I executed that code using : ./executable_file filestem > outputfile

Where I have got my desired output inside outputfile

Now I want to take that executable and run within a python code.

I am trying like :

import subprocess
import sys
filestem = sys.argv[1];
subprocess.run(['/home/dev/executable_file', filestem , 'outputfile'])

But it is unable to create the outputfile. I think some thing should be added to solve the > issue. But unable to figure out. Please help.

Dev
  • 576
  • 3
  • 14
  • Does this answer your question? [How do I get all of the output from my .exe using subprocess and Popen?](https://stackoverflow.com/questions/12600892/how-do-i-get-all-of-the-output-from-my-exe-using-subprocess-and-popen) – Shunya Dec 07 '21 at 12:52

2 Answers2

3

subprocess.run has optional stdout argument, you might give it file handle, so in your case something like

import subprocess
import sys
filestem = sys.argv[1]
with open('outputfile','wb') as f:
    subprocess.run(['/home/dev/executable_file', filestem],stdout=f)

should work. I do not have ability to test it so please run it and write if it does work as intended

Daweo
  • 31,313
  • 3
  • 12
  • 25
0

You have several options:

NOTE - Tested in CentOS 7, using Python 2.7

1. Try pexpect:

"""Usage: executable_file argument ("ex. stack.py -lh")"""
import pexpect

filestem = sys.argv[1]
# Using ls -lh >> outputfile as an example
cmd = "ls {0} >> outputfile".format(filestem)
command_output, exitstatus = pexpect.run("/usr/bin/bash -c '{0}'".format(cmd), withexitstatus=True)
if exitstatus == 0:
    print(command_output)
else:
    print("Houston, we've had a problem.")

2. Run subprocess with shell=true (Not recommended):

"""Usage: executable_file argument ("ex. stack.py -lh")"""
import sys
import subprocess

filestem = sys.argv[1]
# Using ls -lh >> outputfile as an example
cmd = "ls {0} >> outputfile".format(filestem)
result = subprocess.check_output(shlex.split(cmd), shell=True)  # or subprocess.call(cmd, shell=True)
print(result)

It works, but python.org frowns upon this, due to the chance of a shell injection: see "Security Considerations" in the subprocess documentation.

3. If you must use subprocess, run each command separately and take the SDTOUT of the previous command and pipe it into the STDIN of the next command:

p = subprocess.Popen(cmd, stdin=PIPE, stdout=PIPE)
stdout_data, stderr_data = p.communicate()
p = subprocess.Popen(cmd, stdin=stdout_data, stdout=PIPE)
etc...

Good luck with your code!

Rob G
  • 673
  • 3
  • 10
  • Why is pexpect recommended when there are straightforward ways of doing this with the standard library? By the way, subprocess has evolved considerably in Python 3.x. – jkr Dec 07 '21 at 14:08
  • You are right in both cases; I just recommended Pexpect out of all the code I tested. I edited my answer to reflect that. – Rob G Dec 07 '21 at 14:55