How do you read stdin and stdout in python?
You can use:
Both Quick sys.stdin examples compatible with Python 2 and 3, Windows, UnixYou just need to
We can see that
file exampleSay you have a file,
Longer answerHere's a complete, easily replicable demo, using two methods, the builtin function, To begin with, let's create a file for inputs:
And using the code we've already seen, we can check that we've created the file:
Here's the help on
Builtin function, input (raw_input in Python 2)The builtin function Thus, here's how you can use
And let's print it back out to ensure it's as we expect:
Again, And when And on Linux/Unix, we can pipe from cat:
Or we can just redirect the file from stdin:
We can also execute the module as a script:
Here's the help on the builtin
|