Python's "with open() as" Pattern
In this lesson, I’m going to cover Python’s
with open() as pattern, otherwise known as the context manager pattern, which I think is the most important basic pattern for working with files in Python, because it’s what allows you to create and read from existing files in your filesystem.
And then once that file is open, that’s when you can do operations on that file, like write to it or read from it. So the basic file modes—and there are many more, but these are the three that I’ll show you to start with—are write mode,
'w', which wipes the existing file content, creating the file if it doesn’t already exist, and then allows you to write to the file.
And then there is append mode,
'a', which writes to the end of the file, so it does not wipe the existing file content. So, those are the three basic file modes that I’ll explore throughout the rest of this lesson.
In this lesson, I’m going to use an incredibly simple sample directory. It’s just a directory called
test that’s completely empty, because I’m going to be the one creating all of these files in the Python REPL.
So let’s head over to that Python REPL. At the moment I’m in the bare terminal, just because I want to show you that, first, I’m in the
test directory, as I said I would be, and that, second, this has nothing in it, because I just executed the Bash command
ls and, as you can see, there’s no output.
And now I’m in the Python REPL, and we can change that
ls output now to have an actual text file in it. So, if you remember from the slide a second ago, what I can do is I can say something like
'test.txt' as the file name and then I’ll open it in write mode so that I can write to it, and so that it’s created, because
test.txt does not yet exist.
And then I need to say
with open() as, and then I give this a name. Normally I find it convenient to say something like
write_file, so that I remember what exactly it is that I’m writing to, in this case.
I can say something like,
"Hello, I'm a file!" And when you exit out of this, it won’t be quite obvious that anything in particular has happened, but it has been written to and you can check that by saying
with open('test.txt') in read mode
as read_file, and then I can just say
print(read_file.read()), and the
.read() function just returns all of the content of that file.
So as you can see, this is a really existing file and it does have the content
"Hello, I'm a file!" There’s one further mode that I showed you, so I’ll show you now,
"test.txt" in append mode
as write_file, and then I can say
So, that’s how you can open, and read from, and write to a file in a couple of different ways. One last thing that I want to cover in this lesson is why this
with open() as pattern is important, because you can say
write_file = open('test.txt') in write mode, and then you can say
and that will actually return to you—the
write_file.write() function returns the number of characters written there. So you can say—you can do—this, but the problem is that then if you don’t say
write_file.close(), then you will be leaving this file resource open, which your operating system will interpret as needing to have that file open, and it will kind of snarl up your operating system resources.
05:20 That may not seem that important but, really, when you’re building large applications, it’s incredibly important to manage your resources carefully. The context manager syntax allows you to do that really quickly and easily, and you don’t have to ever worry about, “Oh, did I close it here? Do I need to close it there?” Anything that’s in this block is already wrapped by that.
So, that’s how you can use the
with open() as pattern to create, and read from, and write to files. In the next lesson, I’ll cover how to get a directory listing: how to look at all of the things that are in your directory that have already been created.
Become a Member to join the conversation.