1 00:00:01,470 --> 00:00:02,650 - [Instructor] Of course, another thing 2 00:00:02,650 --> 00:00:04,730 that you'll want to do in Python 3 00:00:04,730 --> 00:00:09,150 is be able to read the contents of a file into your program 4 00:00:09,150 --> 00:00:11,060 for processing purposes. 5 00:00:11,060 --> 00:00:13,250 So, let's take a look at a with statement 6 00:00:13,250 --> 00:00:15,640 that opens a file for reading. 7 00:00:15,640 --> 00:00:16,890 Now, as you can see here, 8 00:00:16,890 --> 00:00:18,620 at the beginning of this with statement, 9 00:00:18,620 --> 00:00:22,130 we are once again using the built-in open function 10 00:00:22,130 --> 00:00:27,130 to open up the accounts.txt file that we previously created 11 00:00:27,310 --> 00:00:30,720 that had five fake bank accounts in it. 12 00:00:30,720 --> 00:00:34,610 And, in this case, we are opening the file for reading. 13 00:00:34,610 --> 00:00:38,290 The mode r is opening a text file for reading. 14 00:00:38,290 --> 00:00:40,980 And this open command will work 15 00:00:40,980 --> 00:00:43,560 only if the file already exists 16 00:00:43,560 --> 00:00:46,780 and can be found at the specified location. 17 00:00:46,780 --> 00:00:49,920 Now, because I'm not specifying a location here, 18 00:00:49,920 --> 00:00:54,810 it is going to assume the file is in the current folder 19 00:00:54,810 --> 00:00:58,730 from which I am executing iPython at the moment. 20 00:00:58,730 --> 00:01:02,330 So, indeed it is, as I showed you in a preceding video. 21 00:01:02,330 --> 00:01:04,770 So we're gonna open up that file for reading 22 00:01:04,770 --> 00:01:07,870 and we're going to assign it to the variable accounts 23 00:01:07,870 --> 00:01:11,210 so that we can use that in the sweep below. 24 00:01:11,210 --> 00:01:13,930 Now, let me go ahead and execute this first 25 00:01:13,930 --> 00:01:15,470 just so you can see the results. 26 00:01:15,470 --> 00:01:18,950 As you can see, we have three columns of information 27 00:01:18,950 --> 00:01:21,760 and we've formatted the data in those columns 28 00:01:21,760 --> 00:01:25,060 to be left-aligned in the first two columns 29 00:01:25,060 --> 00:01:28,070 and right-aligned in the third column. 30 00:01:28,070 --> 00:01:31,710 So first of all, we printed the column heads 31 00:01:31,710 --> 00:01:35,490 and we only did that if the file was opened correctly. 32 00:01:35,490 --> 00:01:38,070 If the file is not opened correctly 33 00:01:38,070 --> 00:01:40,690 because, for example, the file doesn't exist, 34 00:01:40,690 --> 00:01:42,460 an exception would have occurred 35 00:01:42,460 --> 00:01:45,480 and the with statement would have terminated immediately. 36 00:01:45,480 --> 00:01:49,220 So, by putting the print statement in the with statement, 37 00:01:49,220 --> 00:01:52,170 it will be displayed only if we successfully 38 00:01:52,170 --> 00:01:53,610 opened the file. 39 00:01:53,610 --> 00:01:56,700 Now, as you can see, we're printing a formatted string, 40 00:01:56,700 --> 00:01:57,720 an f string. 41 00:01:57,720 --> 00:01:59,120 And in the first placeholder, 42 00:01:59,120 --> 00:02:03,540 we have the string Account left-aligned in a field of 10. 43 00:02:03,540 --> 00:02:04,940 In the second placeholder, 44 00:02:04,940 --> 00:02:08,410 we have the string Name left-aligned in a field of 10. 45 00:02:08,410 --> 00:02:10,140 And in the last column, 46 00:02:10,140 --> 00:02:14,590 we have the string Balance right-aligned in a field of 10. 47 00:02:14,590 --> 00:02:17,430 And that's going to enable us to right-align 48 00:02:17,430 --> 00:02:21,940 all of the numeric values and line up their decimal points. 49 00:02:21,940 --> 00:02:24,660 Now, the important piece in the body 50 00:02:24,660 --> 00:02:27,270 of the with statement here is this for loop, 51 00:02:27,270 --> 00:02:32,160 which is iterating through the file object that we created. 52 00:02:32,160 --> 00:02:35,710 One of the interesting things about file objects in Python 53 00:02:35,710 --> 00:02:37,470 is that they are iterable. 54 00:02:37,470 --> 00:02:39,410 And in the case of a text file, 55 00:02:39,410 --> 00:02:44,120 this automatically reads one line at a time from the file. 56 00:02:44,120 --> 00:02:46,890 So, for each of the records in accounts, 57 00:02:46,890 --> 00:02:50,280 for each line of text in the accounts file object, 58 00:02:50,280 --> 00:02:54,000 we are going to tell that string to split, 59 00:02:54,000 --> 00:02:57,750 which will give me a list of the pieces of information 60 00:02:57,750 --> 00:03:00,320 which were separated by spaces 61 00:03:00,320 --> 00:03:04,770 in the actual records of text that were written to the file 62 00:03:04,770 --> 00:03:06,090 in the first place. 63 00:03:06,090 --> 00:03:10,450 We will then unpack that list, which has three values in it, 64 00:03:10,450 --> 00:03:12,350 into account, name, and balance 65 00:03:12,350 --> 00:03:14,410 which is the specific order 66 00:03:14,410 --> 00:03:17,590 in which each record of information was written. 67 00:03:17,590 --> 00:03:19,320 First, we had an account number. 68 00:03:19,320 --> 00:03:20,940 Then a last name. 69 00:03:20,940 --> 00:03:22,210 Then a balance. 70 00:03:22,210 --> 00:03:26,940 And finally, we will then print out that set of information 71 00:03:26,940 --> 00:03:28,390 for the current record 72 00:03:28,390 --> 00:03:30,660 and, once again, we're using an f string. 73 00:03:30,660 --> 00:03:34,610 So the account number will be left-aligned in a field of 10, 74 00:03:34,610 --> 00:03:36,630 underneath the string Account. 75 00:03:36,630 --> 00:03:40,520 The name value will be left-aligned in a field of 10, 76 00:03:40,520 --> 00:03:42,740 under the column head Name. 77 00:03:42,740 --> 00:03:45,100 And the balance value will be right-aligned 78 00:03:45,100 --> 00:03:46,320 in a field of 10. 79 00:03:46,320 --> 00:03:49,620 And remember, these are all string pieces of information, 80 00:03:49,620 --> 00:03:52,850 so I'm not using floating-point formatting here. 81 00:03:52,850 --> 00:03:55,380 I'm simply redisplaying the text 82 00:03:55,380 --> 00:03:57,600 that was read from the file. 83 00:03:57,600 --> 00:03:59,810 If we had subsequently read the text 84 00:03:59,810 --> 00:04:03,130 and converted it into a numeric value, 85 00:04:03,130 --> 00:04:05,600 only then would I use numeric formatting 86 00:04:05,600 --> 00:04:07,500 in this particular case. 87 00:04:07,500 --> 00:04:09,500 Now what's going to happen to this for loop 88 00:04:09,500 --> 00:04:12,320 is it will keep reading from the file 89 00:04:12,320 --> 00:04:15,630 until the end of file indicator 90 00:04:15,630 --> 00:04:18,800 is emitted from reading from the file. 91 00:04:18,800 --> 00:04:20,410 So that's taken care of for you 92 00:04:20,410 --> 00:04:22,210 at the operating system level, 93 00:04:22,210 --> 00:04:23,570 and, once that happens, 94 00:04:23,570 --> 00:04:25,920 the for loop automatically terminates. 95 00:04:25,920 --> 00:04:27,690 And once the for loop terminates, 96 00:04:27,690 --> 00:04:29,310 the with statement will terminate 97 00:04:29,310 --> 00:04:34,160 and automatically close the accounts.txt file. 98 00:04:34,160 --> 00:04:37,500 Now, you may be wondering whether you have to iterate 99 00:04:37,500 --> 00:04:40,390 one line at a time through a text file. 100 00:04:40,390 --> 00:04:41,890 It turns out that you don't. 101 00:04:41,890 --> 00:04:45,500 There is a method built into the file object 102 00:04:45,500 --> 00:04:47,580 called read lines, 103 00:04:47,580 --> 00:04:50,180 which will read the entire text file 104 00:04:50,180 --> 00:04:52,500 and give you back a list of strings 105 00:04:52,500 --> 00:04:56,590 representing the individual lines of text within the file. 106 00:04:56,590 --> 00:04:59,370 However, keep in mind that if the text file 107 00:04:59,370 --> 00:05:02,900 is extremely large, that could take time 108 00:05:02,900 --> 00:05:05,980 and could take up a lot of memory, as well. 109 00:05:05,980 --> 00:05:09,750 Whereas iterating one line at a time through the text file 110 00:05:09,750 --> 00:05:13,770 is accessing the data on demand, if you will, 111 00:05:13,770 --> 00:05:17,860 giving you only one string at a time from the file, 112 00:05:17,860 --> 00:05:19,650 enabling you to process that 113 00:05:19,650 --> 00:05:22,730 and then basically forget about it at that point 114 00:05:22,730 --> 00:05:24,663 and move on to the next record.