简体   繁体   English

如何在 python 脚本的终端中获取存储在文件(例如 *.inp)中的多个“用户输入”

[英]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.

相关问题 如何从终端获取python脚本的输入文件? - How to take input file from terminal for python script? Python:为文件设置多个属性(例如系统、隐藏) - Python: Setting Multiple Attributes for a file (e.g. System, Hidden) 如何在终端中将特定的python版本设置为默认版本,但要保留使用另一个(例如conda)作为软件包的可能性 - How to set a particular python version as default in terminal, but to keep the possibility to use another (e.g. conda) for a package Abaqus python 脚本,如何为具有多个集成点的元素(例如 C3D20R)“添加数据” - Abaqus python script, how to 'addData' for elements with multiple integration points (e.g. C3D20R) Python3 将用户输入字符串解释为原始字节(例如 \x41 == "A") - Python3 interpret user input string as raw bytes (e.g. \x41 == "A") 如何以交互方式(例如使用终端)干预 for 循环 - How to interactively (e.g. using the terminal) intervene a for loop 如何创建一个脚本,该脚本将接收用户输入并在使用python 3.6的终端命令中使用该脚本? - How do I create a script that will take user input and use it in a terminal command using python 3.6? 使用Java for Android平台评估脚本(例如Python) - Evaluate a script (e.g. Python) in Java for Android platform 全屏终端output(例如在网格上) - Fullscreen terminal output (e.g. on a grid) 如何将第一个处理图像(例如 Canny 过滤器)的 output 作为另一个处理过滤器的输入? - How do I take the output of the first processed image(e.g., Canny Filter) as input to another process filter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM