1 00:00:00,610 --> 00:00:03,080 So I've changed the script a little bit. 2 00:00:03,080 --> 00:00:05,980 I've imported sys which I'm using down here. 3 00:00:05,980 --> 00:00:08,720 I'll talk about what argv does in just a minute. 4 00:00:08,720 --> 00:00:10,710 And I've added this function called show. 5 00:00:10,710 --> 00:00:14,936 This is what we are going to use to print out the things that we've saved. 6 00:00:14,936 --> 00:00:18,047 What is sys.argv? 7 00:00:18,047 --> 00:00:23,930 Sys.argv is all of the arguments after your filename. 8 00:00:23,930 --> 00:00:29,778 So if I do python remember.py, and I pass in and 9 00:00:29,778 --> 00:00:35,210 juice boxes that's a common shopping thing for 10 00:00:35,210 --> 00:00:38,770 me, and I come over here to the database, we'll see juice boxes added in. 11 00:00:38,770 --> 00:00:42,620 Juice boxes here is what's in argv. 12 00:00:42,620 --> 00:00:45,360 This is argv zero. 13 00:00:45,360 --> 00:00:46,350 This is argv one. 14 00:00:46,350 --> 00:00:49,620 And this is argv two and so on. 15 00:00:49,620 --> 00:00:54,400 So, when I do this space.join.sys.argv.1: 16 00:00:54,400 --> 00:00:59,240 that grabs everything after remember.py and joins it together with the space. 17 00:00:59,240 --> 00:01:01,510 So that's how we can take all those words. 18 00:01:01,510 --> 00:01:05,180 What I'm doing is I'm checking to see if the first word is --list. 19 00:01:05,180 --> 00:01:08,890 And if it is, then I want to run this show function. 20 00:01:08,890 --> 00:01:11,590 But show currently doesn't do anything. 21 00:01:11,590 --> 00:01:13,770 So let's fill that out, make it do stuff. 22 00:01:13,770 --> 00:01:19,320 All right, so we've already talked about how to use a context manager. 23 00:01:19,320 --> 00:01:20,820 So let's do that. 24 00:01:22,340 --> 00:01:28,130 So we'll say with open("database.txt", 25 00:01:28,130 --> 00:01:34,100 and now I can do r but that's the default so if I don't specify it, it still does r. 26 00:01:34,100 --> 00:01:36,600 And, we'll call if file, and 27 00:01:36,600 --> 00:01:41,080 then inside of here we wanna print out all the stuff that's in the file. 28 00:01:41,080 --> 00:01:43,100 We just have the file open. 29 00:01:43,100 --> 00:01:45,740 So, how do we print out everything that's in that file? 30 00:01:45,740 --> 00:01:51,460 Well, the cool this is, Python treats files kind of like they are iterables. 31 00:01:51,460 --> 00:01:54,860 They are iterables, but it treats them like they're a list or something. 32 00:01:54,860 --> 00:02:01,980 So I can do for line in file: print(line) all right, 33 00:02:01,980 --> 00:02:08,410 and I don't need to close the file because the context processor handles that for me. 34 00:02:08,410 --> 00:02:14,885 So let's save that and so lets do python remember.py --list. 35 00:02:14,885 --> 00:02:18,225 And check that out, there's all of our stuff. 36 00:02:18,225 --> 00:02:19,210 Let's run this one more time. 37 00:02:20,570 --> 00:02:22,660 There we go, there's our stuff that we wanted to get. 38 00:02:22,660 --> 00:02:25,980 Orange Juice, milk, a sweater, a sword, and juice boxes. 39 00:02:25,980 --> 00:02:30,570 I'm hopeful I can go to a store that has all of those things at once. 40 00:02:30,570 --> 00:02:31,480 So cool. 41 00:02:31,480 --> 00:02:33,520 There's all of our stuff. 42 00:02:33,520 --> 00:02:38,980 I have two other methods that I can use to control how stuff is read though. 43 00:02:38,980 --> 00:02:42,970 Instead of writing these into our file, let's just play with them in the terminal. 44 00:02:42,970 --> 00:02:46,980 So I'm gonna actually slide that most of the way up, and I'm gonna clear, and 45 00:02:46,980 --> 00:02:48,020 I'm gonna go into Python. 46 00:02:49,470 --> 00:02:53,970 So, I have a file that has all the teachers in it, it's called teachers.txt. 47 00:02:53,970 --> 00:02:56,620 And I'm gonna mess with that one. 48 00:02:56,620 --> 00:02:57,818 So I'm gonna open it up. 49 00:02:57,818 --> 00:03:01,034 open('teachers.txt'). 50 00:03:01,034 --> 00:03:03,854 I could pass in the r flag for reading it, but 51 00:03:03,854 --> 00:03:07,374 I don't need to do that because it's read by default. 52 00:03:07,374 --> 00:03:09,932 And let's do file.read(10). 53 00:03:09,932 --> 00:03:13,220 I wanna read 10 bytes of this file. 54 00:03:14,480 --> 00:03:20,880 And the 10 is the number of bytes and I get Nick Petti, so, cool new name, Nick. 55 00:03:20,880 --> 00:03:24,280 And so, the cool thing is that this lets me just read parts of a file. 56 00:03:24,280 --> 00:03:29,190 If I know that I can only process, say, 1,024 bytes at a time, 57 00:03:29,190 --> 00:03:33,240 then I can read in just 1,024 bytes at a time. 58 00:03:33,240 --> 00:03:34,050 So, that's pretty cool. 59 00:03:34,050 --> 00:03:35,320 It's pretty handy. 60 00:03:35,320 --> 00:03:38,410 And you can read the entire file if you want, 61 00:03:38,410 --> 00:03:42,820 by doing it with no argument or with a negative number. 62 00:03:44,110 --> 00:03:48,810 If we do file.read, we get the rest of the file and it's the rest of the file because 63 00:03:48,810 --> 00:03:52,300 here's the t that should have been on Nick's name. 64 00:03:52,300 --> 00:03:56,190 It's not because when you have a file object in Python, 65 00:03:56,190 --> 00:04:00,710 there is a little argument, a little value, on that file object. 66 00:04:00,710 --> 00:04:04,700 That's a pointer to where we've read to in the file. 67 00:04:04,700 --> 00:04:06,920 We know how much we read up to. 68 00:04:06,920 --> 00:04:09,810 So we know that we've read 10 bytes. 69 00:04:09,810 --> 00:04:12,357 So when we say, okay, keep reading that file, 70 00:04:12,357 --> 00:04:15,520 it starts at the 11th byte and keeps moving. 71 00:04:15,520 --> 00:04:19,890 The way that we reset that, is we can use file.seek. 72 00:04:19,890 --> 00:04:22,430 And if we just do file.seek it has to have an argument so 73 00:04:22,430 --> 00:04:25,420 we tell it where to go and we say go back to 0. 74 00:04:25,420 --> 00:04:27,730 And it gives us back where it currently is. 75 00:04:27,730 --> 00:04:29,790 It's currently at 0. 76 00:04:29,790 --> 00:04:30,780 All right, so that's cool. 77 00:04:30,780 --> 00:04:37,830 And then you do file.read(15) and we've got the 11 characters of Nick's name, 78 00:04:37,830 --> 00:04:41,550 the one new line character, and then three characters of Amit's name. 79 00:04:41,550 --> 00:04:43,410 That adds up to 15 bytes total. 80 00:04:44,450 --> 00:04:45,990 All right, that's really handy. 81 00:04:45,990 --> 00:04:49,117 I want to show you one last thing about reading from files though. 82 00:04:50,420 --> 00:04:53,984 Let's go back to the beginning and lots of times we have a file and 83 00:04:53,984 --> 00:04:58,800 it has a whole lot of lines in it and each line needs to be a thing on its own. 84 00:04:58,800 --> 00:05:02,790 So, the way that we can do that is by using the read-lines method. 85 00:05:02,790 --> 00:05:06,890 There's also a read-line method, which would just read one line. 86 00:05:06,890 --> 00:05:09,340 But read-lines is way more useful. 87 00:05:09,340 --> 00:05:17,410 So let's do lines=file.readlines and then let's look at the len of the lines. 88 00:05:17,410 --> 00:05:20,050 So cool, it says there's 17 things in there. 89 00:05:20,050 --> 00:05:27,380 All right, let's look at our teachers.txt and yep, there are 17 lines. 90 00:05:27,380 --> 00:05:32,934 And the cool thing is we could do like, for line in lines: 91 00:05:32,934 --> 00:05:38,520 print(line[::-1]). 92 00:05:38,520 --> 00:05:40,990 And there's everybody's names backwards. 93 00:05:40,990 --> 00:05:42,400 So that's kind of cool. 94 00:05:42,400 --> 00:05:46,220 There's lots of other things you can do with files so go and explore them. 95 00:05:48,290 --> 00:05:51,180 Using files for storing data is a really handy thing for 96 00:05:51,180 --> 00:05:52,120 a lot of programming problems. 97 00:05:52,120 --> 00:05:54,820 It's also a great way to get output from a script 98 00:05:54,820 --> 00:05:58,140 if you need the script to run in the background or something like that. 99 00:05:58,140 --> 00:06:00,560 You might want to use the logging library for that though, so 100 00:06:00,560 --> 00:06:03,411 be sure to check it out before jumping straight to writing to files yourself. 101 00:06:04,730 --> 00:06:07,860 If you're working with files, there's a good chance you're going to need to use 102 00:06:07,860 --> 00:06:10,910 CRV or JSON files or some other format. 103 00:06:10,910 --> 00:06:14,420 Keep an eye out for another workshop about both of those file formats. 104 00:06:14,420 --> 00:06:17,370 As always, if you have an idea for future workshops, let me know. 105 00:06:17,370 --> 00:06:18,670 Thanks for watching and I'll see you soon