Python Forum

Full Version: Running another python script from a python script
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello, I have been trying to fix this for a long time, but couldn't do it

Let me first explain what I am trying to do -

I have a python script which prints a question to terminal, then waits for answer, then prints an answer and so on until user enters QUIT. That script uses print() to print and input() to get the input. That script is complete.


I have to use flask to set up a webpage. The first time we visit the home page, we want to execute the above python script, and redirect to second page, then everything takes place on second page i.e. get what it printed on terminal, and display it on the same page, get what user typed in the input box, send that input to the second print and repeat.

I used subprocess module to run the second script. I used fw = open('somefile','w+') and fr = open('somefile','r') and while using Popen, put stdout = fw, stdin = PIPE. And I use fr.read() to get the data from file.

But it is not working. My fr.read() is reading nothing(empty) even though the lines are correctly written on 'somefile'(seen in text editor).

Needed help in how to tackle the situation.
If more information is required, please tell me
in the second program add:
import program_that_asks_question as p1
you can then execute functions from the program_that_asks_question, like:
zz = p1.function_name()
Rather that using subprocess, you need to control 1st program from second
(program names would help with explanation)