Practical Guide to Python

Working with Files

Practical Guide to Python

Check out a free preview of the full Practical Guide to Python course

The "Working with Files" Lesson is part of the full, Practical Guide to Python course featured in this preview video. Here's what you'd learn in this lesson:

Nina explains how Python works with files on the operating system and the different modes available when opening a file. A context manager is a safer way to work with files since it will automatically close the file even if an exception occurs.

Preview
Close

Transcript from the "Working with Files" Lesson

[00:00:00]
>> I'm not gonna dive into this too much, but there are a few important concepts and syntax that I would like you to be familiar with. When you work with files, there are different modes for opening them, don't recognize them or sorry, don't memorize them. Instead, check out this cheat sheet for when you need it, and forget it, it's kind of useless knowledge.

[00:00:29]
The important one though, is r which is open for reading, that is the default. So if you don't pass in a file mode that's the one that it's going to be. There are other file modes like truncate, open for writing, but truncate the file there's a text mode.

[00:00:51]
Generally, you'll probably only be working with r or w or occasionally a, which means open this file I would like to write to it. But if that file already exists, append to the end of it instead of overwriting it. So opening a file in Python looks something like this.

[00:01:14]
You call open on it. Remember that will open a file in read mode by default. If you want to open a file for writing but replace any existing file use mode w or if you want to open a file for reading and writing, but append to the end of a file if it already exists use mode a.

[00:01:37]
Now the thing about files is, you want to call close on any open file that you have to release the handle that your program has on that file back to the operating system. Otherwise you're locking it up. And if your program crashes before you happen to call close, you might end up in some pretty weird states.

[00:02:01]
So Python has some shortcuts and tax called a context manager. And a context manager is like taking a block of code that depends on some resource, like a file. And there's, some stuff that happens under the hood when you use this syntax this keyword with, that will then close the file for you, and also take care of any cleanup if this happens to throw an exception.

[00:02:33]
So if you are familiar with other programming languages, it's similar to a try and finally block but it's a lot cleaner to look at. So you can think of context managers as code that just automagically provisions a resource before your code runs and then cleans it up afterward.

[00:02:53]
And that open function that we saw earlier that works as a context manager. And so opening a file can look like this. And then we don't have to call close on that file every time and litter our code with open, close, open, close. We can use this with keyword and know that that closing is happening for us.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now