views:

207

answers:

2

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.

Thanks, V

+9  A: 

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
Crast
Thanks for the quick reply. I created script to test the subprocess.call method, hardcoding in filenames etc, and it worked fine, now I think I need to learn a little more python and Tkinter, so that I can figure out how to put it all together, my OO programing skills are little rusty...
volting
Ok, I managed to put it all together, which proved a lot simpler then I thought. I Was having some problems which just turned out to be syntactical errors, my dev enviroment is pretty primative at the moment basically consists of notepad++, I will look into a more comprehensive solution when I have time, anyway im rambling...Now that Iv'e achieved my initial goal, Im interested in how I could achieve a higher level of integration, Iv used the .pyw extension so that the dos window only pops up for a split second, but It would be nice if it didn't at all. Is there an easy way to do....
volting
...this without going to the trouble of creating a dll and calling the c functions from within the python app.
volting
There's no easy way, unfortunately. If you ran your python app in a console instead of via pythonw, then at least the apps you run would -probably- not pop up additional consoles, which is less annoying if you have say, a lot of files to process. But even that is not guaranteed. If the popup is undesired, it might be worth investing the time to fix the C app's entry point to not require a console or to turn it into a library.
Crast
I originally used the .py extension instead of... but I if felt it was better not having the console there the whole time. At the moment only one file can be selected at a time, the c console app literally only takes milliseconds to run so it doesn't popup for much more then that, so I dont think its a major issue, but I will be adding an option for batch processing which would cause the problem you described, i.e multiple consoles..Your first solution interests me, but I'm not really sure how it can be implementd, Ive searched but without any look.. could you elaborate..
volting
ok, read your last post again, ...you are talking about using the win32Api, right?
volting
+2  A: 

There are a number of ways you could acheive this, but in your case the requirements seems so straightforward all you need is to use Popen from the subprocess module.

 process = subprocess.Popen(['myutil', file_location]...)
 process.wait()

If you need to get the results back to the caller, use a PIPE for the stdout and read the resulting data from it when the subprocess has finished.

jkp