简体   繁体   中英

How can I send multiple “user input” to python script from another python script?

For example I have this file

#file2.py
while True:
    name = input("Your name: ")
    print("Hello ", name)

I want to run file2.py from other python file and just send inputs and get outputs. I used subprocess module and stdin and stdout but it close file2.py after one input and I got error for second input.

You can use import . Name the file from which you want to open file2.py as file.py and enter your code there, with import file2 at where you want file2.py to be opened. Save the file in the same directory as file2.py. So, file.py should look like this:

{code}
import file2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM