Python Forum
Running another python script from a python script
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Running another python script from a python script
#1
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
Reply
#2
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Autonomous Python Script Leyo 6 1,871 Apr-01-2022, 09:37 AM
Last Post: Axel_Erfurt
  Re-write BASH script to Python script popi75 5 2,364 Apr-30-2021, 03:52 PM
Last Post: metulburr
  My script returns no values Shay3534 7 3,263 Dec-04-2020, 09:47 PM
Last Post: Shay3534
  Create process to control python script kobibi11 5 2,555 Jun-07-2020, 08:19 AM
Last Post: kobibi11
  Opening files in same directory as script Emekadavid 3 2,514 May-28-2020, 06:50 PM
Last Post: Emekadavid
  Error in Python script garvind25 10 5,828 May-25-2020, 08:50 PM
Last Post: GOTO10
  running python in cmd on win10 rny1python 6 3,051 May-18-2020, 11:36 PM
Last Post: deanhystad
  An interesting Role-Play-Game battle script NitinL 4 3,324 Apr-02-2020, 03:51 AM
Last Post: NitinL
  Please help with this simple tic tac toe script ErtYwek 2 1,870 Mar-06-2020, 06:42 PM
Last Post: ndc85430
  Running Python Packages mojobadshah 10 3,628 Jan-30-2020, 10:28 PM
Last Post: mojobadshah

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020