1 00:00:00,880 --> 00:00:03,250 - Before we move ahead to the portion 2 00:00:03,250 --> 00:00:05,800 of this lesson on handling exceptions, 3 00:00:05,800 --> 00:00:08,110 I just wanted to deal with a couple of 4 00:00:08,110 --> 00:00:11,040 remaining open items for file processing. 5 00:00:11,040 --> 00:00:14,456 First of all, we've only shown you the R and W 6 00:00:14,456 --> 00:00:17,463 file open modes but there are several other 7 00:00:17,463 --> 00:00:20,220 file open modes these are all of the ones 8 00:00:20,220 --> 00:00:23,520 for text file processing and each one of these 9 00:00:23,520 --> 00:00:26,690 also has a version with the letter V in it 10 00:00:26,690 --> 00:00:31,323 so R-V, W-V, A-V, R-V+, W-V+, and A-V+ 11 00:00:32,670 --> 00:00:35,600 and each of those is for binary files 12 00:00:35,600 --> 00:00:39,080 if in fact you have the need to process binary files 13 00:00:39,080 --> 00:00:41,980 so you can do reading, writing and appending and 14 00:00:41,980 --> 00:00:43,880 there's versions of each of those that 15 00:00:43,880 --> 00:00:47,170 allow you to both read and write in the file as well 16 00:00:47,170 --> 00:00:49,600 so you can pause the video and take a look at these 17 00:00:49,600 --> 00:00:53,380 descriptions if you'd like to look at them in more detail. 18 00:00:53,380 --> 00:00:56,170 Now separately, there's a couple of additional methods 19 00:00:56,170 --> 00:01:00,140 that you may find useful if you're reading from a file, 20 00:01:00,140 --> 00:01:03,054 there is a read method if it's a text file 21 00:01:03,054 --> 00:01:04,940 it will read characters, 22 00:01:04,940 --> 00:01:07,480 if it's a binary file it will read bytes 23 00:01:07,480 --> 00:01:11,210 and the number of characters or bytes that you want to read 24 00:01:11,210 --> 00:01:14,750 is specified as an integer argument to the method 25 00:01:14,750 --> 00:01:17,140 or if you provide no arguments, 26 00:01:17,140 --> 00:01:21,020 it will simply read the entire contents of the file. 27 00:01:21,020 --> 00:01:24,440 There's also the read line method which returns one line 28 00:01:24,440 --> 00:01:27,010 of text as a string and that will 29 00:01:27,010 --> 00:01:30,090 include the new line character if there is one. 30 00:01:30,090 --> 00:01:33,878 This particular method will return an empty string 31 00:01:33,878 --> 00:01:38,600 if end file is encountered and if you're writing to a file, 32 00:01:38,600 --> 00:01:42,810 a particularly handy method is the writelines method. 33 00:01:42,810 --> 00:01:47,730 If you have a sequence of strings, writelines will output 34 00:01:47,730 --> 00:01:50,520 the entire sequence to the file. 35 00:01:50,520 --> 00:01:52,670 So you may recall when we first began 36 00:01:52,670 --> 00:01:56,100 introducing file processing at the beginning of this lesson, 37 00:01:56,100 --> 00:01:58,950 we called the write method many times over 38 00:01:58,950 --> 00:02:01,660 to write one record at a time. 39 00:02:01,660 --> 00:02:04,680 Well, if we simply took the strings that represented 40 00:02:04,680 --> 00:02:08,390 each of those records and threw them into a list for example 41 00:02:08,390 --> 00:02:11,450 we could have written them all out in one operation 42 00:02:11,450 --> 00:02:14,860 by passing that list to the writelines method. 43 00:02:14,860 --> 00:02:19,851 Now, as you may guess, there are classes that Python uses 44 00:02:19,851 --> 00:02:24,730 to implement the file objects that we received 45 00:02:24,730 --> 00:02:27,320 back from the open function each time 46 00:02:27,320 --> 00:02:30,490 we opened a file for reading or for writing. 47 00:02:30,490 --> 00:02:32,496 Those classes are defined in the 48 00:02:32,496 --> 00:02:36,140 Python Standard Library module called I-O 49 00:02:36,140 --> 00:02:38,199 and if you're interested in looking at the details 50 00:02:38,199 --> 00:02:40,870 of those classes and all of the different methods 51 00:02:40,870 --> 00:02:44,086 that they provide you can find that documentation here 52 00:02:44,086 --> 00:02:46,543 on the Python dot org website.