I have a c console app which converts a c file to a html file, the c file location is passed to the program as a command line argument.(the app is for the windows platform)
What I would like to do is have a python gui app to allow the user to select a file and pass the location of the file to the c app for processing.
I already know how to create a basic python gui with tkinter, but I can't figure out or find any usefull info on how I would go about intgrating the two programs.
Maybe its possible to pipe the string to the c app with the pOpen() method? (but I can't figure out how...)
Note: Im new to python so code examples might be helpful rather then just description, (as Im not familiar with all of the python libraries etc,) although any help at all would be appreciated.
You probably want the subprocess module.
At the very minimum:
import subprocess
retcode = subprocess.call(["/path/to/myCprogram", "/path/to/file.c"])
if retcode == 0:
print "success!"
This will run the program with the arguments, and then return its return code.
Note that subprocess.call will block until the program has completed, so if it's not one which runs quickly, your entire Tkinter GUI will stop redrawing until it's completed.
For more advanced use, you may want to use subprocess.Popen
. This will require you polling until the command has completed, but allows you to do it with less blocking.
If your C program prints the HTML to standard out, you will need to pipe the output like so:
proc = subprocess.Popen(["/path/to/myCprogram", "/path/to/file.c"],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, err_output = proc.communicate()
# output will now contain the stdout of the program in a string