I want know how to pass information from one python program to another. Basically, I will open one python program which uses the os.system(file) command to execute another pytgon program. Let's take an example:
The "Parent" program:
import file #file is the child
import os
num=int(input("Enter number: "))
if num%2==0:
os.system('python file.py')
else:
pass
Now the "Child" program:
name=input("Enter Name: ")
age=int(input("Enter age: "))
print("Hi",name)
So in the example, when the user enters an even number, the program starts up the child program, which in turn will ask the user his name and age.
Now my question is this: If I want to bring back the information (name and age) entered in the child program back to the parent program, how do I do it?
os.system
and instead use thesubprocess
module.import child
doesn't make sense if you are going to be calling it as a seperate process. Indeed, what you ideally should do with two python modules like this is write them so that they can be imported, and their public API exposes whatever functionality you want. In which case, just ditchos
andsubprocess
and justimport file; value = file.foobar(whatever)
and you havevalue