Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Printing to File Streams

print() normally prints to the <stdout> file stream. You can change this behavior using the file parameter. You can access the <stdout> stream directly from the sys library:

>>>
>>> import sys
>>> result = sys.stdout.write('hello\n')
hello
>>> result
6

You can also change where print() prints to:

with open('file.txt', mode='w') as file_object:
    print('hello world', file=file_object)

00:00 In the previous lesson, I covered sep, end, and flush and how you use those arguments to change the output of print(). In this lesson, I’ll be talking about how to print to things besides the screen. print() doesn’t actually implement writing characters to the screen.

00:16 It’s a wrapper to code that does this work. Operating systems have three standard streams. You can think of a stream as a way for characters to go in and out of the system.

00:28 The system streams are stdin (standard in) for reading input, stdout (standard out) for displaying output, and stderr (standard error) for displaying errors. In most applications,

00:39 stdout and stderr both go to the console screen. Inside of Python’s sys library, you can see the streams themselves. If you’re familiar with file objects, you’ll notice the mode='r' there is similar to when you use open() with an 'r' to say you want to read from a file. That’s because all of the streams are file-like objects. stdout has mode='w' because it’s output, and stderr—also mode='w' for output.

01:11 Let’s look at that inside of the REPL. Import sys and call in the stdout stream the .write() method. This works just like print().

01:23 In fact, this is what print() is doing. Notice that one of the differences here, though, is print() automatically appends a '\n'.write() does not, so you have to put it in there explicitly. The .write() method returns the number of bytes that it spit out to the stream. In this case, the result is 6. I know 'hello' only has five characters, but that '\n' actually counts. Five plus the one character for the '\n', gives us the 6.

01:53 In addition to the other arguments, print() also supports an argument called file. file takes a file-like object and wherever it points is where the resulting output goes.

02:03 It defaults to the stdout stream. Instead of writing to a stream, you can also write to a file. Open the file in write mode, you get your file object, pass that file object in as the file argument, and print() is now printing to that file instead of to the screen. One word of warning though: print() and stdout are both meant to handle string data.

02:27 If you’re working with binary data, things go wrong here. There are other ways of writing binary content to files.

02:35 Next up, I’ll show you how to add methods to your classes to control what gets displayed when they get sent to print().

eshivaprasad on May 17, 2020

Are there situations in which sys.stdout.write() is preferable than print()?

Christopher Trudeau RP Team on May 19, 2020

Not commonly. There are a couple bizarre corner cases (later in the course I mock out print and then need to print something to screen), but these are rare and far between.

Become a Member to join the conversation.