Python — stdin, stdout и stderr
Прежде чем читать эту статью, давайте разберемся, что такое термины stdin , stdout и stderr .
Стандартный ввод — это дескриптор файла, который пользовательская программа читает, чтобы получить информацию от пользователя. Мы передаем ввод на стандартный ввод (stdin).
Стандартный вывод — программа пользователя записывает обычную информацию в этот дескриптор файла. Вывод возвращается через стандартный вывод (stdout).
Стандартная ошибка — программа пользователя записывает информацию об ошибке в этот дескриптор файла. Ошибки возвращаются через стандартную ошибку (stderr).
Python предоставляет нам файловые объекты, представляющие stdin, stdout и stderr. Давайте посмотрим, как мы могли бы работать с этими объектами, чтобы использовать ввод и вывод программы.
1. sys.stdin
Модуль Python sys предоставляет нам все три файловых объекта для stdin, stdout и stderr. В качестве объекта входного файла мы используем sys.stdin . Это похоже на файл, где вы можете открывать и закрывать его, как и любые другие файлы в Python.
Давайте разберемся на простом примере:
import sys stdin_fileno = sys.stdin # Keeps reading from stdin and quits only if the word 'exit' is there # This loop, by default does not terminate, since stdin is open for line in stdin_fileno: # Remove trailing newline characters using strip() if 'exit' == line.strip(): print('Found exit. Terminating the program') exit(0) else: print('Message from sys.stdin: ---> <> <---'.format(line))
Hi Message from sys.stdin: ---> Hi Hello from AskPythonВыше фрагмент кода продолжает чтение входных данных из stdin и выводит сообщение на консоли ( stdout ) до тех пор , слово exit не встречаются.
Примечание. Обычно мы не закрываем объект файла stdin по умолчанию, хотя это разрешено. Итак, stdin_fileno.close() — допустимый код Python.
Теперь, когда мы немного знаем о stdin , перейдем к stdout .
2. sys.stdout
В качестве объекта выходного файла мы используем sys.stdout . Он похож на sys.stdin , но напрямую отображает все, что написано в нем, в консоли.
В приведенном ниже фрагменте показано, что мы получаем вывод в консоль, если пишем в sys.stdout .
import sys stdout_fileno = sys.stdout sample_input = ['Hi', 'Hello from AskPython', 'exit'] for ip in sample_input: # Prints to stdout stdout_fileno.write(ip + '\n')Hi Hello from AskPython exit3. sys.stderr
Это похоже на sys.stdout потому что он также sys.stdout непосредственно выводит в консоль. Но разница в том, что он печатает только сообщения об исключениях и ошибках. (Вот почему он называется стандартной ошибкой).
Проиллюстрируем это на примере.
import sys stdout_fileno = sys.stdout stderr_fileno = sys.stderr sample_input = ['Hi', 'Hello from AskPython', 'exit'] for ip in sample_input: # Prints to stdout stdout_fileno.write(ip + '\n') # Tries to add an Integer with string. Raises an exception try: ip = ip + 100 # Catch all exceptions except: stderr_fileno.write('Exception Occurred!\n')Hi Exception Occurred! Hello from AskPython Exception Occurred! exit Exception Occurred!Как вы можете заметить, для всех входных строк мы пытаемся добавить к Integer, что вызовет исключение. Мы перехватываем все такие исключения и печатаем еще одно сообщение отладки с помощью sys.stderr .
Перенаправление в файл
Мы можем перенаправить дескрипторы файлов stdin , stdout и stderr в любой другой файл (дескриптор файла). Это может быть полезно, если вы хотите регистрировать события в файле без использования какого-либо другого модуля, такого как Logging.
Приведенный ниже фрагмент перенаправляет вывод ( stdout ) в файл с именем Output.txt .
Итак, мы не увидим ничего, напечатанного в консоли, потому что теперь это печатается в самом файле! В этом суть перенаправления вывода. Вы «перенаправляете» вывод в другое место. (На этот раз в Output.txt , а не в консоль)
import sys # Save the current stdout so that we can revert sys.stdou after we complete # our redirection stdout_fileno = sys.stdout sample_input = ['Hi', 'Hello from AskPython', 'exit'] # Redirect sys.stdout to the file sys.stdout = open('Output.txt', 'w') for ip in sample_input: # Prints to the redirected stdout (Output.txt) sys.stdout.write(ip + '\n') # Prints to the actual saved stdout handler stdout_fileno.write(ip + '\n') # Close the file sys.stdout.close() # Restore sys.stdout to our old saved file handler sys.stdout = stdout_filenoroot@ubuntu:~# python3 output_redirection.py Hi Hello from AskPython exit root@ubuntu:~# cat Output.txt Hi Hello from AskPython exitКак видите, мы распечатали вывод как в консоль, так и в Output.txt .
Сначала мы сохраняем исходный sys.stdout обработчик файла sys.stdout в другую переменную. Нам это нужно не только для восстановления sys.stdout в старом обработчике (указывающем на консоль), но мы также можем печатать на консоль, используя эту переменную!
Обратите внимание, что после записи в файл мы закрываем его, аналогично тому, как мы закрываем файл, потому что этот файл все еще был открыт.
Наконец, мы восстанавливаем обработчик sys.stdout в консоли, используя переменную stdout_fileno .
Аналогичный процесс можно выполнить для перенаправления ввода и ошибок, заменив sys.stdout на sys.stdin или sys.stderr и работая с sys.stderr и исключениями вместо вывода.
How to Read from stdin in Python
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
1. Using sys.stdin to read from standard input
Python sys module stdin is used by the interpreter for standard input. Internally, it calls the input() function. The input string is appended with a newline character (\n) in the end. So, you can use the rstrip() function to remove it. Here is a simple program to read user messages from the standard input and process it. The program will terminate when the user enters “Exit” message.
import sys for line in sys.stdin: if 'Exit' == line.rstrip(): break print(f'Processing Message from sys.stdin **********') print("Done")
Hi Processing Message from sys.stdin *****Hi ***** Hello Processing Message from sys.stdin *****Hello ***** Exit Done
Notice the use of rstrip() to remove the trailing newline character so that we can check if the user has entered “Exit” message or not.
2. Using input() function to read stdin data
We can also use Python input() function to read the standard input data. We can also prompt a message to the user. Here is a simple example to read and process the standard input message in the infinite loop, unless the user enters the Exit message.
while True: data = input("Please enter the message:\n") if 'Exit' == data: break print(f'Processing Message from input() **********') print("Done")
The input() function doesn’t append newline character to the user message.
3. Reading Standard Input using fileinput module
We can also use fileinput.input() function to read from the standard input. The fileinput module provides utility functions to loop over standard input or a list of files. When we don’t provide any argument to the input() function, it reads arguments from the standard input. This function works in the same way as sys.stdin and adds a newline character to the end of the user-entered data.
import fileinput for fileinput_line in fileinput.input(): if 'Exit' == fileinput_line.rstrip(): break print(f'Processing Message from fileinput.input() **********') print("Done")
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.