Reading from Files6:19 with Kenneth Love
If you write to a file, there's a really good chance you're going to want to read from that file. Here's how to read in the contents of a file in Python.
open(filename, mode="r")opens a file. More info in the docs.
file.read(bytes=-1) would read the entire contents of the file. You can control the number of bytes read by passing in an integer. Relatedly,
file.seek() will move the read/write pointer to another part of the file.
file.readlines() reads the entire file into a list, with each line as a list item.
The context manager pattern for dealing with files is:
with open("my_file.txt", "r") as file: file.read(10)
For more about
sys.argv, check out the docs.
So I've changed the script a little bit. 0:00 I've imported sys which I'm using down here. 0:03 I'll talk about what argv does in just a minute. 0:05 And I've added this function called show. 0:08 This is what we are going to use to print out the things that we've saved. 0:10 What is sys.argv? 0:14 Sys.argv is all of the arguments after your filename. 0:18 So if I do python remember.py, and I pass in and 0:23 juice boxes that's a common shopping thing for 0:29 me, and I come over here to the database, we'll see juice boxes added in. 0:35 Juice boxes here is what's in argv. 0:38 This is argv zero. 0:42 This is argv one. 0:45 And this is argv two and so on. 0:46 So, when I do this space.join.sys.argv.1: 0:49 that grabs everything after remember.py and joins it together with the space. 0:54 So that's how we can take all those words. 0:59 What I'm doing is I'm checking to see if the first word is --list. 1:01 And if it is, then I want to run this show function. 1:05 But show currently doesn't do anything. 1:08 So let's fill that out, make it do stuff. 1:11 All right, so we've already talked about how to use a context manager. 1:13 So let's do that. 1:19 So we'll say with open("database.txt", 1:22 and now I can do r but that's the default so if I don't specify it, it still does r. 1:28 And, we'll call if file, and 1:34 then inside of here we wanna print out all the stuff that's in the file. 1:36 We just have the file open. 1:41 So, how do we print out everything that's in that file? 1:43 Well, the cool this is, Python treats files kind of like they are iterables. 1:45 They are iterables, but it treats them like they're a list or something. 1:51 So I can do for line in file: print(line) all right, 1:54 and I don't need to close the file because the context processor handles that for me. 2:01 So let's save that and so lets do python remember.py --list. 2:08 And check that out, there's all of our stuff. 2:14 Let's run this one more time. 2:18 There we go, there's our stuff that we wanted to get. 2:20 Orange Juice, milk, a sweater, a sword, and juice boxes. 2:22 I'm hopeful I can go to a store that has all of those things at once. 2:25 So cool. 2:30 There's all of our stuff. 2:31 I have two other methods that I can use to control how stuff is read though. 2:33 Instead of writing these into our file, let's just play with them in the terminal. 2:38 So I'm gonna actually slide that most of the way up, and I'm gonna clear, and 2:42 I'm gonna go into Python. 2:46 So, I have a file that has all the teachers in it, it's called teachers.txt. 2:49 And I'm gonna mess with that one. 2:53 So I'm gonna open it up. 2:56 open('teachers.txt'). 2:57 I could pass in the r flag for reading it, but 3:01 I don't need to do that because it's read by default. 3:03 And let's do file.read(10). 3:07 I wanna read 10 bytes of this file. 3:09 And the 10 is the number of bytes and I get Nick Petti, so, cool new name, Nick. 3:14 And so, the cool thing is that this lets me just read parts of a file. 3:20 If I know that I can only process, say, 1,024 bytes at a time, 3:24 then I can read in just 1,024 bytes at a time. 3:29 So, that's pretty cool. 3:33 It's pretty handy. 3:34 And you can read the entire file if you want, 3:35 by doing it with no argument or with a negative number. 3:38 If we do file.read, we get the rest of the file and it's the rest of the file because 3:44 here's the t that should have been on Nick's name. 3:48 It's not because when you have a file object in Python, 3:52 there is a little argument, a little value, on that file object. 3:56 That's a pointer to where we've read to in the file. 4:00 We know how much we read up to. 4:04 So we know that we've read 10 bytes. 4:06 So when we say, okay, keep reading that file, 4:09 it starts at the 11th byte and keeps moving. 4:12 The way that we reset that, is we can use file.seek. 4:15 And if we just do file.seek it has to have an argument so 4:19 we tell it where to go and we say go back to 0. 4:22 And it gives us back where it currently is. 4:25 It's currently at 0. 4:27 All right, so that's cool. 4:29 And then you do file.read(15) and we've got the 11 characters of Nick's name, 4:30 the one new line character, and then three characters of Amit's name. 4:37 That adds up to 15 bytes total. 4:41 All right, that's really handy. 4:44 I want to show you one last thing about reading from files though. 4:45 Let's go back to the beginning and lots of times we have a file and 4:50 it has a whole lot of lines in it and each line needs to be a thing on its own. 4:53 So, the way that we can do that is by using the read-lines method. 4:58 There's also a read-line method, which would just read one line. 5:02 But read-lines is way more useful. 5:06 So let's do lines=file.readlines and then let's look at the len of the lines. 5:09 So cool, it says there's 17 things in there. 5:17 All right, let's look at our teachers.txt and yep, there are 17 lines. 5:20 And the cool thing is we could do like, for line in lines: 5:27 print(line[::-1]). 5:32 And there's everybody's names backwards. 5:38 So that's kind of cool. 5:40 There's lots of other things you can do with files so go and explore them. 5:42 Using files for storing data is a really handy thing for 5:48 a lot of programming problems. 5:51 It's also a great way to get output from a script 5:52 if you need the script to run in the background or something like that. 5:54 You might want to use the logging library for that though, so 5:58 be sure to check it out before jumping straight to writing to files yourself. 6:00 If you're working with files, there's a good chance you're going to need to use 6:04 CRV or JSON files or some other format. 6:07 Keep an eye out for another workshop about both of those file formats. 6:10 As always, if you have an idea for future workshops, let me know. 6:14 Thanks for watching and I'll see you soon 6:17
You need to sign up for Treehouse in order to download course files.Sign up