我对来自 C/C++ 的 python 还很陌生,我想知道如何让我的“main.py”重新调整/使用从 bash shell 给出的输入:
python main.py < text.txt
(文件为纯文本)
最佳答案
从 sys.stdin
读取:
import sys
sys.stdin.read()
成为 file-like object ,您可以使用它的阅读功能或简单地遍历输入行:
for line in sys.stdin:
print line
关于Python 命令行 'file input stream',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6415382/