What does sys.stdin read?

后端 未结 5 1350
感动是毒
感动是毒 2020-12-05 11:17

I get how to open files, and then use Python\'s pre built in functions with them. But how does sys.stdin work?

for something in sys.stdin:
    some stuff he         


        
5条回答
  •  天命终不由人
    2020-12-05 11:52

    To get a grasp how sys.stdin works do following:

    create a simple python script, let's name it "readStdin.py":

    import sys
    lines = sys.stdin.readlines()
    print (lines)
    

    Now open console any type in:

    echo "line1 line2 line3" | python readStdin.py
    

    The script outputs:

    ['"line1 line2 line3" \n']
    

    So, the script has read the input into list (named 'lines'), including the new line character produced by 'echo'. That is.

提交回复
热议问题