[英]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.我想从其他 python 文件中运行 file2.py 并只发送输入并获取输出。 I used subprocess module and stdin and stdout but it close file2.py after one input and I got error for second input.我使用了子进程模块以及标准输入和标准输出,但它在一次输入后关闭了 file2.py,第二次输入出现错误。
You can use import
.您可以使用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.将要从中打开 file2.py 的文件命名为 file.py 并在此处输入您的代码,在您希望打开 file2.py 的位置使用import file2
。 Save the file in the same directory as file2.py.将文件保存在与 file2.py 相同的目录中。 So, file.py should look like this:所以,file.py 应该是这样的:
{code}
import file2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.