I'm trying to write a script thats spawns a fortran program that needs std file input i.e. you would open it in bash with: ./program < input How can I do this in python? I have tried the os.spawnl() and it spawns the process but the input does not work.

Recommended Answers

All 8 Replies

Search this forum or google for subprocess. It's the new de facto method of spawning processes. There's plenty of examples if you look.

Thanks for the help. Now i got a problem with the stdin in the subprocess module. It works fine with a fortran application but with my own written C-program it fails with a segfault. I call it with:

proc2 = subprocess.Popen("/home/samush/Documents/kandidat_arbete/python/cr_to_fr/fortreaderc",shell=True,stdin = subprocess.PIPE)

without the stdin it runs but the script cant pass any input to the process. How can I solve this? (I use the scanf() and printf() for in/output in the C-program)
Thanks!

There should be a comma after PIPE (I'm guessing that this is a tuple), and then a communicate statement

proc2 = subprocess.Popen("/home/samush/Documents/kandidat_arbete/python/cr_to_fr/fortreaderc",
          shell=True,
          stdin = subprocess.PIPE,)
proc2.communicate('\tstdin: to stdin\n')

http://blog.doughellmann.com/2007/07/pymotw-subprocess.html

Thats strange because it worked without the comma on the fortran app. I have a communicate statement after it. The thing is I still have a segfault when running the script...and it hangs on the line in my C program where I have getchar(). I now this because I have some output before that line. Besides when I have

proc2 = subprocess.Popen("/home/samush/Documents/kandidat_arbete/python/cr_to_fr/fortreaderc",shell=True,stdin = subprocess.PIPE, stdout= subprocess.PIPE,)

i only get a segfault without any output.
What is wrong??

Actually I was wrong the segfault comes between two printf() statements before gethchar in the C-program.

i only get a segfault without any output.
What is wrong??

Your C program is most likely at fault. Simply recode it in Python instead of just calling it from Python.

I don't understand what you mean 'at fault'. The program runs without any problem if you open it from bash. I have run the program quite a lot....but when I try to run it with a python script I get the segfault.... I dont think the segfault is from my program because when the segfault comes the program has only declared some variables and printed a string with printf() and before it comes to the next print statement there is segfault...Besides it also runs without any problem from the script if I don't use the stdin = subprocess.PIPE, but then I have to type the input manually.

You should be able to call it with
subprocess.call("./program < " + input_string, shell=True)
Using subprocess to send the input; since you also have a printf, you might have to include an output pipe as well, and you might as well use a stderr pipe which may be more specific about errors. I don't know if c/scanf empties or otherwise modifies the input stream on startup, so you may also have to wait for the first printf output before sending input to the scanf to avoid any program startup mangling.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.