Je ne suis qu'un débutant. Je viens de lire sur le module sys. Je n'arrive pas à le saisir. Quelqu'un peut-il expliquer ce que sont ces deux-là?
Can anyone give me a quick tutorial in stdin and stdout in Python 3? - Stack Overflow
Preferred method of reading from stdin?
python - How do I read from stdin? - Stack Overflow
Writing to an actively running Python script's STDIN
Videos
stdin and stdout are file-like objects provided by the OS. In general, when a program is run in an interactive session, stdin is keyboard input and stdout is the user's tty, but the shell can be used to redirect them from normal files or piped output from and input to other programs.
input() is used to prompt the user for typed input. In the case of something like a programming puzzle, it's normally assumed that stdin is redirected from a data file, and when the input format is given it's usually best to use sys.stdin.read() rather than prompting for input with input(). input() is intended for interactive user input, it can display a prompt (on sys.stdout) and use the GNU readline library (if present) to allow line editing, etc.
print() is, indeed, the most common way of writing to stdout. There's no need to do anything special to specify the output stream. print() writes to sys.stdout if no alternate file is given to it as a file= parameter.
When you run your Python program, sys.stdin is the file object connected to standard input (STDIN), sys.stdout is the file object for standard output (STDOUT), and sys.stderr is the file object for standard error (STDERR).
Anywhere in the documentation you see references to standard input, standard output, or standard error, it is referring to these file handles. You can access them directly (sys.stdout.write(...), sys.stdin.read() etc.) or use convenience functions that use these streams, like input() and print().
For the Spotify puzzle, the easiest way to read the input would be something like this:
import sys
data = sys.stdin.read()
After these two lines the input for your program is now in the str data.
Hi, just wondering if using input() or sys.stdin is the preferred method for reading from stdin in python. What are the differences and what is more readable/pythonesque?
# Using input()
while True:
try:
line = input()
...
except EOFError:
break
# Using sys.stdin
for line in sys.stdin:
line = line.strip()
...Use the fileinput module:
import fileinput
for line in fileinput.input():
pass
fileinput will loop through all the lines in the input specified as file names given in command-line arguments, or the standard input if no arguments are provided.
Note: line will contain a trailing newline; to remove it use line.rstrip().
There's a few ways to do it.
sys.stdinis a file-like object on which you can call functionsreadorreadlinesif you want to read everything or you want to read everything and split it by newline automatically. (You need toimport sysfor this to work.)If you want to prompt the user for input, you can use
raw_inputin Python 2.X, and justinputin Python 3.If you actually just want to read command-line options, you can access them via the sys.argv list.
You will probably find this Wikibook article on I/O in Python to be a useful reference as well.
I have a python script that takes in input string form the user, and returns an output string (it's a bot). But, I want to turn this script into a 'service' of sorts, and therefore I want an external app to write to to a running version of the script (with an active PID) and receive a response, as though a user were typing the input at the CLI. Here is the script:
while True:
try:
user_input = input('You: ')
bot_response = bot.get_response(user_input)
print('Bot:', bot_response, bot_response.confidence)How can I send a string to this script and receive a response. Note: I don't want to put a wrapper around the script, because the script has some import statements in the header that take a while to load. Once the script loads the necessary libraries, it's pretty speedy, but the loading process makes the script too slow to function with a wrapper.