Read a File from redirected stdin with python

后端 未结 3 892
孤街浪徒
孤街浪徒 2021-01-13 10:33

I am trying to read the content of a text file that was redirected stdin via the command line, and send it by the Internet when the receiver has to assemble it back to it\'s

3条回答
  •  天涯浪人
    2021-01-13 10:52

    Instead of breaking, you just want to continue to the next line. The iterator will stop automatically when it reaches the end of the file.

    import sys
    result = ""
    for line in sys.stdin:
        stripped = line.strip()
        if not stripped:
            continue
        result += stripped
    

提交回复
热议问题