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.

asked Dec 7, 2021 at 12:49
1

2 Answers 2

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

answered Dec 7, 2021 at 12:57
Sign up to request clarification or add additional context in comments.

Comments

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!

answered Dec 7, 2021 at 14:06

2 Comments

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.
You are right in both cases; I just recommended Pexpect out of all the code I tested. I edited my answer to reflect that.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.