1 00:00:00,000 --> 00:00:05,880 In this section, we will use Python's built-in open function to create a file object, 2 00:00:05,880 --> 00:00:08,870 and obtain the data from a "txt" file. 3 00:00:08,870 --> 00:00:12,760 We will use Python's open function to get a file object. 4 00:00:12,760 --> 00:00:16,740 We can apply a method to that object to read data from the file. 5 00:00:16,740 --> 00:00:22,080 We can open the file, Example1.txt, as follows. 6 00:00:22,080 --> 00:00:24,040 We use the open function. 7 00:00:24,040 --> 00:00:26,590 The first argument is the file path. 8 00:00:26,590 --> 00:00:28,790 This is made up of the file name, 9 00:00:28,790 --> 00:00:30,650 and the file directory. 10 00:00:30,650 --> 00:00:32,920 The second parameter is the mode. 11 00:00:32,920 --> 00:00:36,090 Common values used include 'r' for reading, 12 00:00:36,090 --> 00:00:39,160 'w' for writing, and 'a' for appending. 13 00:00:39,160 --> 00:00:41,320 We will use 'r' for reading. 14 00:00:41,320 --> 00:00:43,930 Finally, we have the file object. 15 00:00:43,930 --> 00:00:48,250 We can now use the file object to obtain information about the file. 16 00:00:48,250 --> 00:00:52,140 We can use the data attribute name to get the name of the file. 17 00:00:52,140 --> 00:00:55,680 The result is a string that contains the name of the file. 18 00:00:55,680 --> 00:01:00,180 We can see what mode the object is in using the data attribute mode, 19 00:01:00,180 --> 00:01:02,890 and 'r' is shown representing read. 20 00:01:02,890 --> 00:01:06,720 You should always close the file object using the method close. 21 00:01:06,720 --> 00:01:08,780 This may get tedious sometimes, 22 00:01:08,780 --> 00:01:11,310 so let's use the "with" statement. 23 00:01:11,310 --> 00:01:13,780 Using a "with" statement to open a file is 24 00:01:13,780 --> 00:01:17,120 better practice because it automatically closes the file. 25 00:01:17,120 --> 00:01:20,270 The code will run everything in the indent block, 26 00:01:20,270 --> 00:01:21,980 then closes the file. 27 00:01:21,980 --> 00:01:25,720 This code reads the file, Example1.txt. 28 00:01:25,720 --> 00:01:29,100 We can use the file object, "File1." 29 00:01:29,100 --> 00:01:31,850 The code will perform all operations in 30 00:01:31,850 --> 00:01:36,030 the indent block then close the file at the end of the indent. 31 00:01:36,030 --> 00:01:42,950 The method "read" stores the values of the file in the variable "file_stuff" as a string. 32 00:01:42,950 --> 00:01:45,070 You can print the file content. 33 00:01:45,070 --> 00:01:47,670 You can check if the file content is closed, 34 00:01:47,670 --> 00:01:50,530 but you cannot read from it outside the indent. 35 00:01:50,530 --> 00:01:54,460 But you can print the file content outside the indent as well. 36 00:01:54,460 --> 00:01:56,530 We can print the file content. 37 00:01:56,530 --> 00:01:58,290 We will see the following. 38 00:01:58,290 --> 00:02:00,500 When we examine the raw string, 39 00:02:00,500 --> 00:02:02,230 we will see the " ." 40 00:02:02,230 --> 00:02:05,400 This is so Python knows to start a new line. 41 00:02:05,400 --> 00:02:10,850 We can output every line as an element in a list using the method "readlines." 42 00:02:10,850 --> 00:02:14,400 The first line corresponds to the first element in the list. 43 00:02:14,400 --> 00:02:19,180 The second line corresponds to the second element in the list, and so on. 44 00:02:19,180 --> 00:02:23,810 We can use the method "readline" to read the first line of the file. 45 00:02:23,810 --> 00:02:25,260 If we run this command, 46 00:02:25,260 --> 00:02:31,390 it will store the first line in the variable "file_stuff" then print the first line. 47 00:02:31,390 --> 00:02:34,250 We can use the method "readline" twice. 48 00:02:34,250 --> 00:02:35,830 The first time it's called, 49 00:02:35,830 --> 00:02:41,380 it will save the first line in the variable "file_stuff," and then print the first line. 50 00:02:41,380 --> 00:02:43,000 The second time it's called, 51 00:02:43,000 --> 00:02:44,750 it will save the second line in the variable 52 00:02:44,750 --> 00:02:48,600 "file_stuff," and then print the second line. 53 00:02:48,600 --> 00:02:53,250 We can use a loop to print out each line individually as follows. 54 00:02:53,250 --> 00:02:56,930 Let's represent every character in a string as a grid. 55 00:02:56,930 --> 00:03:00,110 We can specify the number of characters we would like to read from 56 00:03:00,110 --> 00:03:03,650 the string as an argument to the method "readlines." 57 00:03:03,650 --> 00:03:06,700 When we use a four as an argument in the method 58 00:03:06,700 --> 00:03:11,080 "readlines," we print out the first four characters in the file. 59 00:03:11,080 --> 00:03:13,090 Each time we call the method, 60 00:03:13,090 --> 00:03:15,050 we will progress through the text. 61 00:03:15,050 --> 00:03:17,920 If we call a method with the arguments 16, 62 00:03:17,920 --> 00:03:21,560 the first 16 characters are printed out, and then the new line. 63 00:03:21,560 --> 00:03:23,930 If we call the method a second time, 64 00:03:23,930 --> 00:03:26,640 the next five characters are printed out. 65 00:03:26,640 --> 00:03:30,940 Finally, if we call the method the last time with the argument nine, 66 00:03:30,940 --> 00:03:33,730 the last nine characters are printed out. 67 00:03:33,730 --> 00:03:38,000 Check out the labs for more examples of methods and other file types. 68 00:03:38,000 --> 00:03:43,000 (Music)