Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

Functions: Input and Output

00:01 Input and Output.

00:05 In this section, you’re going to be taking a look at features which are classified under input and output, and particularly in this case, it will be print() and open(). print() is probably the first thing you came across ever, which you can see here with print() the archetypal "Hello World!", and obviously that returns Hello World!.

00:26 But there are other values which we can use with print(), as you can see here in bpython. And one of them in particular can be really useful, which is end.

00:37 You can see end there, and it defaults to the newline character ('\n'). This is going to be shown to you in the context of a program to see how useful it can be when printing out multiple values to the command line and you don’t want them on separate lines. Flicking over to code, you’re going to see a program which is a toy example, but will show you how useful this can be.

01:01 So, all this program is going to do is print out the numbers from 0 to 99. We have the range() constructor here, which is just going to print those out.

01:13 Seeing that in action, you can see it running

01:17 and we get those numbers. You can see we’ve got a hundred lines printed out, and it can be difficult to see everything which is happening onscreen at once. If you don’t want that, you can make use of the end variable. So, we can set this. As default, end is the newline character we saw earlier on in the course. Instead of that, let’s change it just to be a space (' ').

01:43 Now when it’s run, you can see that all of the numbers are available onscreen at the same time, even to the point where we don’t get a newline at the end of the print, and our prompt is on the same line as that last print.

01:55 You can see that can be fixed with just a blank print() statement and running it again. You can clearly see where the program stops, and this can be really useful for dealing with programs where you have a lot of printout which comes onto the screen at the same time, and you may end up scrolling from hundreds of lines.

02:12 This can make it much easier to deal with.

02:16 Next up, open(). Here, open() is going to be used in a fairly simplistic way. This is not what you’d do typically, because normally you would use a thing called a context manager, as you will see later on. But here, the example will make sure the file is closed by explicitly closing the file object. Here, we’ll start with the file object being defined using the open() function.

02:42 It takes two values, the name of the file and the mode in which it will be opened—in this case, write ('w'). Here we have 'test.txt' and that’s going to be opened in the write mode.

02:58 Once this has been executed, we have a file object that we can now access, and one of the methods that it supports is .write().

03:07 We’re going to write just some text, as seen there. We’re going to write 'Hello World!' and you’ll see there’s a newline entered at the end with '\n'.

03:19 And finally, you should close the file object, in this case with the .close() method. As mentioned previously, typically you would use a context manager, but this is going to ensure the file is manually closed in this simple example.

03:35 Here you can see the program running. There’s no direct output, but looking at a list of the directory you can see test.txt exists. And if I use more to examine the contents of test.txt, you can see the text Hello World! has been created. Now clearly, this simple example could be expanded by adding more text, but you can see the basics of how open() works to open a file on the computer’s hard drive. The other way you’re going to see here is in read mode.

04:05 To make the code difference clear, I’m going to comment out the original writing part of the program and see how it’s opened in read mode ('r').

04:15 You can see that the open() line is very similar with open('test.txt', 'r'). And this time, the file object gives us the .readlines() method, which reads all the lines of the file as a list.

04:33 And then we’ll ensure the file is closed. So this time, once it’s run, you can see that we have a list of the text which is present, including the newline character.

04:46 And that’s the very basics of reading using open(). If you want to look further into this, Real Python has a course on reading and writing files which will show you much more about how open() works, the different modes available to it, using context managers, and how to deal with the contents of different types of files.

Avatar image for sr25889431

sr25889431 on March 3, 2021

Hello!

At 4:40, why is it important that we ensure the file is closed after we open it in read mode?

Avatar image for Ricky White

Ricky White RP Team on March 5, 2021

This is the explanation from the written version of this course.

It’s important to remember that it’s your responsibility to close the file. In most cases, upon termination of an application or script, a file will be closed eventually. However, there is no guarantee when exactly that will happen. This can lead to unwanted behavior including resource leaks. It’s also a best practice within Python (Pythonic) to make sure that your code behaves in a way that is well defined and reduces any unwanted behavior.

realpython.com/read-write-files-python/#opening-and-closing-a-file-in-python

Avatar image for Dick de Goede

Dick de Goede on Oct. 29, 2022

Hello, when I run the script to read the test file, I get the output [‘Hello World!\n] instead of the literal output from the file. What can cause this?

Avatar image for Dick de Goede

Dick de Goede on Oct. 29, 2022

Sorry, I overlooked the last part that is indeed the expected output. I still wonder however how to get the literal output, but probably I will learn that later :)

Avatar image for Darren Jones

Darren Jones RP Team on Nov. 1, 2022

file.readlines() returns a list, so you could alter this line to

print(file.readlines()[0])

This would print out the first entry in the list, and that would give you the output I think that you’re expecting.

Avatar image for Caylor Willis

Caylor Willis on April 5, 2023

At 4:12 he highlighted then commented out three lines of code. How did he do that as opposed to manually moving the cursor to each line and typing “#”?

Avatar image for Bartosz Zaczyński

Bartosz Zaczyński RP Team on April 5, 2023

@Caylor Willis In Visual Studio Code, as well as many other code editors, you can typically hit Ctrl + / to comment out the highlighted lines at once. It actually works as a toggle switch, so doing it again will uncomment the highlighted or just the current line.

Become a Member to join the conversation.