[英]How to take multiple "user input" stored in a file (e.g., *.inp) in terminal for python script
I want to run the below code in my Linux terminal:我想在我的 Linux 终端中运行以下代码:
python script.py < text.inp
where the script.py is asking for multiple "user inputs" at different lines . script.py 在不同的行中要求多个“用户输入”。 And instead of manual input in the terminal, I want to use the text.inp file, which stored the "user inputs".而不是在终端中手动输入,我想使用存储“用户输入”的 text.inp 文件。
I tried cat command, but it is just reading the lines of the text.inp file, but not using them as input (what we do in manual input).我尝试了cat命令,但它只是读取 text.inp 文件的行,而不是使用它们作为输入(我们在手动输入中所做的)。
Can someone please help me with how to do it?有人可以帮我怎么做吗?
You could do it like that你可以那样做
input_string = input('How to enter the inputs? (0:manual, 1:file) ')
assert input_string in ['0', '1']
if input_string == '0':
inputs = [] # Do your manual inputs, store them in a list
else:
file_name = input('file name? ')
with open(file_name, mode='r') as f:
inputs = f.readlines()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.