Home > Blockchain >  Running a C executable inside a python program
Running a C executable inside a python program

Time:12-08

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.

CodePudding user response:

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

CodePudding user response:

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!

  • Related