Locked lesson.
About this lesson
Exercise files
Download this lesson’s related exercise files.
Opening and Closing Files.docx59.3 KB Opening and Closing Files - Solution.docx
59.1 KB
Quick reference
Opening and Closing Files
In this video we'll learn how to open and close files.
When to use
Use this whenever you need to open or close an external file with Python.
Instructions
To open a file, create a file object:
my_file = open("names.txt", "r")
# the first parameter is the name of the file, the second parameter is the open mode (r, w, a, +, b)
The open file modes (how you will open a file) are as follows:
r = Read only, pointer at beginning of file
w = Write only, overwrites the entire file
a = Appending, pointer at the end of the file
b = Binary, opens binary files
+ = Both reading and writing, pointer at beginning of file
To find the name of an open file:
print(my_file.name)
To find out if a file has been closed:
print(my_file.closed)
To find out the mode of an opened file:
print(my_file.mode)
To close a file:
my_file.close()
Hints & tips
- my_file = open("names.txt", "r") # Open a file
- my_file.close() # Close a file
- 00:04 In this video, I want to talk about opening and closing files and
- 00:07 when we looked at modules we're sort of opening a file, but not really.
- 00:11 We're importing a module into our program.
- 00:14 If you want to open an actual file or close a file,
- 00:17 it's a little bit different and that's what we're going to talk about.
- 00:19 So, first before we get started, let's create a second file, I'm going to right
- 00:22 click here and click here and click New File, and let's just call it names.text.
- 00:27 All right, so let's open this up, double click it, and
- 00:31 I'm just going to type some names, let's go John, Tim, Mary, and Bob.
- 00:36 So I'm going to save this, and so all we have here is just a simple text file,
- 00:40 any kind of file will work.
- 00:41 And we've got some stuff in the file.
- 00:43 So I'm going to close it.
- 00:44 Now let's say we want to open this file and do stuff with it from our Python file.
- 00:49 How would we do that?
- 00:50 Well, we need to create a file object.
- 00:52 And it's super easy to do with Python.
- 00:55 You just create a variable name like always.
- 00:58 Let's go my file.
- 01:00 Call it whatever you want, it doesn't really matter.
- 01:02 And then we type in open, this open function.
- 01:05 And then we need to pass it a few parameters.
- 01:07 So the first parameter is the name of the file.
- 01:10 So let's go names.txt.
- 01:13 And then separate each of these by a comma.
- 01:15 And then next we need to give it the open mode that we're going to use.
- 01:20 And I'll talk about that in just a second.
- 01:21 So I'm going to type in R.
- 01:23 And the third attribute is something called buffering and it's usually zero,
- 01:27 one or a number greater than one.
- 01:30 But we don't usually deal with buffering so
- 01:32 we're just going to sort of ignore that.
- 01:33 So here we have it and if we save this and run it of course nothing's going to
- 01:37 happen, because we've opened our file but we haven't actually done anything with it.
- 01:41 So before we talk about doing stuff with files and we'll do that a little bit later
- 01:45 on in the next couple of videos, I want to talk about this object.
- 01:48 We've created this file object.
- 01:50 There are a few attributes you can do to sort of play with this and
- 01:53 get information out of it.
- 01:55 Once you've opened it, you can find out the name of it.
- 01:58 So let's go print and say my_file.
- 02:01 And then .name.
- 02:03 So if we save this and
- 02:05 run it, we get names.txt because that's the name of our file.
- 02:08 So that's kind of cool, kind of a neat little thing you can do.
- 02:11 You can also say, has that file been closed yet?
- 02:14 We opened it but we didn't actually close it.
- 02:16 You always need to close your files once you open it.
- 02:18 May not remember if it's open or closed so we can type closed.
- 02:22 We save this and run it, we get false because it's not closed.
- 02:25 It's still open because we opened it right here.
- 02:27 And finally we can do the mode.
- 02:29 And I'm going to talk about modes in just a second.
- 02:31 We run this, we see R because that's the mode that I selected.
- 02:35 So I mentioned you can open these files.
- 02:37 You also always need to close them afterwards.
- 02:40 And it's really easy to close a file.
- 02:42 You just go, my_file.close(), and
- 02:44 then pass it, if you're doing the close function.
- 02:48 Then we save this.
- 02:49 Actually, let's take this, and let's change this to closed, save it and run it.
- 02:56 False, it's not been closed yet.
- 02:58 And now if we go like this, save it and run it, we're going to see it's true,
- 03:02 because this line closes the file.
- 03:04 Always at the end you want to sort of put your close statement there.
- 03:08 It's just, sort of, best practices.
- 03:09 Once your program ends, the file will close automatically.
- 03:12 But we want to get in the habit of closing files when we open them.
- 03:15 Very quickly now, I want to talk about this mode.
- 03:18 So what is a mode?
- 03:20 There are three main modes when opening files.
- 03:24 And I'll put these in the resource section.
- 03:28 You have r which is read only, w which is write only.
- 03:33 And a, which is appending.
- 03:35 Now in addition to that, we also have b, which stands for
- 03:39 binary, and plus, which stands for both reading and writing.
- 03:42 And we append these on to either of these three things.
- 03:46 So if you want to open read only in binary, you would do rb right.
- 03:52 If you want to do read only but also writing you would go r+.
- 03:56 If you want to write only but also read, w+.
- 04:00 If you want to write binary and also read, wb+.
- 04:04 So r is the default read only.
- 04:06 And what that does is,
- 04:07 when you open a file in read only mode, it means you can only read that file.
- 04:12 We could only read these names out of that file.
- 04:15 We couldn't save anything else onto the file.
- 04:17 We couldn't do anything with it.
- 04:18 We can just read it.
- 04:19 With write only you can't read it, but you can write stuff to it.
- 04:23 So, and you'll notice when you do that it overwrites the file.
- 04:26 If we called w on this file, names.txt, it would erase everything on there and
- 04:32 replace it with whatever we told it to put in there.
- 04:35 So you got to be careful about that.
- 04:36 With read only the pointer, which is,
- 04:38 think of the mouse, starts at the beginning of the file.
- 04:41 It starts up here and it reads everything down.
- 04:44 With append, the pointer goes at the end and
- 04:47 then you can add another name, right, Steve.
- 04:51 If you're going to append, you append it to the end.
- 04:54 So these are the different modes.
- 04:56 You use them whenever you open your file.
- 04:58 I usually tend to use r+, because then you can read and write.
- 05:02 So if I want to add something to the beginning of the file, if I want to put
- 05:05 a new name up here, I would do r+ because the pointer starts at the beginning.
- 05:10 If I wanted to put that name at the end like that, I would use a or
- 05:15 a+, because then the pointer goes at the end of the file.
- 05:18 So that's how you open a file.
- 05:19 That's how you close the file.
- 05:20 In the next video we'll look at actually reading stuff out of files and
- 05:24 writing things to files.
Lesson notes are only available for subscribers.