What does sys.stdin read?

后端 未结 5 1347
感动是毒
感动是毒 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:53

    According to me sys.stdin.read() method accepts a line as the input from the user until a special character like Enter Key and followed by Ctrl + D and then stores the input as the string.

    Control + D works as the stop signal.

    Example:

    import sys
    
    input = sys.stdin.read()
    print(input)
    tokens = input.split()
    a = int(tokens[0])
    b = int(tokens[1])
    print(a + b)
    

    After running the program enter two numbers delimited by space and after finishing press Control + D once or twice and you will be presented by the sum of the two inputs.

提交回复
热议问题