Functions: Input and Output
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() is probably the first thing you came across ever, which you can see here with
print() the archetypal
"Hello World!", and obviously that returns
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
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.
So, all this program is going to do is print out the numbers from
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
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.
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.
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
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.
Once this has been executed, we have a
file object that we can now access, and one of the methods that it supports is
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
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.
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.
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 (
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.
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.
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.
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?
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 :)
file.readlines() returns a list, so you could alter this line to
This would print out the first entry in the list, and that would give you the output I think that you’re expecting.
Become a Member to join the conversation.
sr25889431 on March 3, 2021
At 4:40, why is it important that we ensure the file is closed after we open it in read mode?