Python 命令行 'file input stream'

标签 python file file-io filestream

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

相关文章:

python - 如何将mysql concat代码更改为pymysql代码

python - 从平面文件中解析出数据

bash - 在 bash 脚本中打开文件

c++ - 文件 IO 到内存 IO

python - 在只读 View (如 HTML 文件)中打开 IPython 笔记本 (*.ipynb)

python - 带参数的 get_absolute_url

python - 如何从 FileField 获取 (txt) 文件内容?

file - 如何在 Go 中获取文件长度?

c - 删除文件指针指向的行的内容

java - Android 应用程序创建不在 Activity 中的内部文件