Locked lesson.
About this lesson
Exercise files
Download this lesson’s related exercise files.
Reading and Writing Files.docx60.6 KB Reading and Writing Files - Solution.docx
59.4 KB
Quick reference
Reading and Writing Files
Once open, you can easily read a file or write to a file.
When to use
Whenver you need to read the contents of a file or write to a file, you'll do it this way.
Instructions
To read and write to a file we use the read() and write() functions.
To read a file:
my_file = open("names.txt", "r") # open a file
my_string = my_file.read() #read that file into a string
To write to a file:
my_file = open("names.txt", "a+") # open the file in append mode
my_file.write("Steve\n") # write Steve to the end of the file with a line break
Hints & tips
- my_file = open("names.txt", "a+) # open a file
- my_string = my_file.read() #read that file into a string
- my_file.write("Steve\n") # write Steve to the end of the file with a line break
- 00:03 In this video, I want to talk about reading to a file and writing to a file.
- 00:08 We're opening our file, it's r+.
- 00:10 And now, we want to pull stuff out of there and do stuff with it.
- 00:13 So how do we do that?
- 00:14 Well, there are two functions that we can use, the read function and
- 00:16 the write function.
- 00:17 And you can imagine what each of those do, and it's very simple,
- 00:20 we just create another string.
- 00:21 Let's just call it my_string equals and
- 00:24 now we want to read the stuff out of my file.
- 00:28 So we go my_file.read and it's a function so we can pass in a parameter.
- 00:33 Now, how much do we want to read from the file?
- 00:36 How many bytes of information do we want to read 10 bytes, 10 characters,
- 00:40 or 100 characters, whatever you want.
- 00:43 If you leave it blank, it tries to read everything in the file.
- 00:45 So more likely than not, you're just going to leave it blank.
- 00:48 What it does is it pulls everything out of the file and
- 00:50 it assigns it into this my_string variable.
- 00:53 So if we want to print out to the screen, my_string, we can do that and we get John,
- 00:58 Tim, Mary and Bob.
- 00:59 So that's one way to read.
- 01:00 Another way to read kind of like to use sometimes is to use just a for
- 01:04 loop to loop through the file.
- 01:06 And so, we could go for letters in my_file,
- 01:10 print letters and letters is just a variable.
- 01:15 You could put X, Y, whatever variable name, doesn't really matter.
- 01:19 If we save this and run it, we get the same John, Tim and Mary.
- 01:22 Here, we're getting a line breaks in between.
- 01:24 It's just a function of the loop thing, but either way, interesting ways to do it.
- 01:28 So now, let's talk about writing to a file, its just as easy and
- 01:32 it depends on how you want to write.
- 01:34 I want to add Steve to the end of this file so
- 01:37 since I want to put Steve at the end, I want to use this append mode, right?
- 01:43 I change this r to a, now I can append and we're going to use the write function,
- 01:47 just like we just used the read function.
- 01:49 Now we use the write function and to use that, just the same,
- 01:51 we just go my_file.write to use the write function and then pass it a parameter.
- 01:58 So what do we want to add?
- 01:59 We want to add Steve, and let's put a line break on there too.
- 02:03 We know the line break from earlier.
- 02:04 So if we save this and
- 02:05 run it, nothing's going to happen because we're not printing anything to the screen.
- 02:09 But if we open our file up again, boom, we see Steve there at the end.
- 02:12 So super, super easy.
- 02:14 Now, let's say we want to add a name to the top, right.
- 02:16 We don't want to use write because if we try to add Steve to the top,
- 02:20 it will overwrite the entire file.
- 02:22 It will erase all of the stuff, and the only thing listed will be Steve and
- 02:26 we don't want that.
- 02:27 So, what we want here is r+, r+ allows us to read and write only.
- 02:33 See this plus, reading and writing and the pointer is at the beginning of the file.
- 02:37 So let's put instead of Steve, let's put Tina and everything else stays the same.
- 02:42 We could just save this and run it again.
- 02:43 Now if we open up our names text, we see Tina, Tim, Mary, Bob and Steve.
- 02:48 Pretty cool, so very quickly, let's do this w thing.
- 02:52 We're going to write only and that overwrites everything as we know, so
- 02:56 let's see.
- 02:56 Let's put Wes in there, save it and run it.
- 03:00 Now if we open up our names, reload,
- 03:03 Wes is the only name listed here because we've overwritten everything else.
- 03:08 Always keep in mind that w is kind of tricky.
- 03:10 I tend not to use that ever because I just don't ever want to overwrite
- 03:14 everything in a file.
- 03:15 If that was the case, I would probably just create a new file, right?
- 03:17 So that's reading, that's writing, very, very simple.
- 03:20 The big thing to learn is these three, r, w, and a, and if the file is binary
- 03:25 you would use b plus this plus thing, most of the time you're going to use like
- 03:29 I said, r+ seems to be the one that I always use and most people tend to use.
- 03:33 And so that's that, so we've looked at opening and closing files,
- 03:37 we looked at reading and writing files.
- 03:39 In the next video, I want to look at renaming and deleting files.
Lesson notes are only available for subscribers.