gpt4 book ai didi

Python 命令行 'file input stream'

转载 作者:太空狗 更新时间:2023-10-29 20:11:40 25 4
gpt4 key购买 nike

我对来自 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com