我有文件 loadMe.txt,我想在脚本 main.py 中使用它。为此,我通过命令从命令行执行脚本:
python main.py < loadMe.txt
如何以这种“<”方式访问我的脚本中的 loadMe.txt?另外运算符“<”是如何命名的?
最佳答案
<
称为“输入重定向”。它使用该文件作为输入源。你可以想到cmd < file
相当于cat file | cmd
.
在Python中,有多种读取方式。基本款input()
命令将读取直到出现新行。您还可以执行以下操作:
import sys
# use `sys.stdin` like the file.
whole_contents = sys.stdin.read()
# Or something like
first_five_chars = sys.stdin.read(5)
关于python - 通过尖括号参数访问加载的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46991927/