Python – Print to File
In this article, we shall look at some of the ways to use Python to print to file.
Method 1: Print To File Using Write()
We can directly write to the file using the built-in function write() that we learned in our file handling tutorial.
with open('output.txt', 'a') as f: f.write('Hi') f.write('Hello from AskPython') f.write('exit')
Output (Assume that output.txt is a newly created file)
[email protected]:~# python output_redirection.py Hi Hello from AskPython exit [email protected]:~# cat output.txt Hi Hello from AskPython exit
Method 2: Redirect sys.stdout to the file
Usually, when we use the print function, the output gets displayed to the console.
But, since the standard output stream is also a handler to a file object, we can route the standard output sys.stdout to point to the destination file instead.
The below code is taken from our previous article on stdin, stdout and stderr. This redirects the print() to the file.
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_fileno
Output (Assume that output.txt is a newly created file)
[email protected]:~# python output_redirection.py Hi Hello from AskPython exit [email protected]:~# cat output.txt Hi Hello from AskPython exit
Method 3: Explicitly print to the file
We can directly specify the file to be printed in the call to print() , by mentioning the file keyword argument.
For example, the below snippet prints to the file output.txt .
print('Hi', file=open('output.txt', 'a')) print('Hello from AskPython', file=open('output.txt', 'a')) print('exit', file=open('output.txt', 'a'))
The file now has the three lines appended to it, and we have successfully printed to output.txt !
Using a context manager
However, this method isn’t the best way to resolve this situation, due to the repeated calls to open() on the same file. This wastes time, and we can do better!
The better way would be to explicitly use a context manager with statement, which takes care of automatically closing the file and using the file object directly.
with open("output.txt", "a") as f: print('Hi', file=f) print('Hello from AskPython', file=f) print('exit', file=f)
This gives the same result as before, appending the three lines to output.txt , but is now much faster, since we don’t open the same file again and again.
Method 4: Use the logging module
We can use Python’s logging module to print to the file. This is preferred over Method 2, where explicitly changing the file streams is not be the most optimal solution.
import logging # Create the file # and output every level since 'DEBUG' is used # and remove all headers in the output # using empty format='' logging.basicConfig(filename='output.txt', level=logging.DEBUG, format='') logging.debug('Hi') logging.info('Hello from AskPython') logging.warning('exit')
This will, by default, append the three lines to output.txt . We have thus printed to the file using logging , which is one of the recommended ways of printing to a file.
References
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 и исключениями вместо вывода.