1 00:00:00,000 --> 00:00:03,930 We can also write to files using the open function. 2 00:00:03,930 --> 00:00:09,660 We will use Python's open function to get a file object to create a text file. 3 00:00:09,660 --> 00:00:13,140 We can apply method write to write data to that file. 4 00:00:13,140 --> 00:00:16,240 As a result, text will be written to the file. 5 00:00:16,240 --> 00:00:21,230 We can create the file Example2.txt as follows. 6 00:00:21,230 --> 00:00:23,000 We use the open function. 7 00:00:23,000 --> 00:00:25,430 The first argument is the file path. 8 00:00:25,430 --> 00:00:27,820 This is made up of the file name. 9 00:00:27,820 --> 00:00:30,160 If you have that file in your directory, 10 00:00:30,160 --> 00:00:33,140 it will be overwritten, and the file directory. 11 00:00:33,140 --> 00:00:36,170 We set the mode parameter to W for writing. 12 00:00:36,170 --> 00:00:38,680 Finally, we have the file object. 13 00:00:38,680 --> 00:00:41,450 As before we use the with statement. 14 00:00:41,450 --> 00:00:45,640 The code will run everything in the indent block then close the file. 15 00:00:45,640 --> 00:00:48,490 We create the file object, File1. 16 00:00:48,490 --> 00:00:50,660 We use the open function. 17 00:00:50,660 --> 00:00:55,790 This creates a file Example2.txt in your directory. 18 00:00:55,790 --> 00:00:57,360 We use the method write, 19 00:00:57,360 --> 00:00:59,360 to write data into the file. 20 00:00:59,360 --> 00:01:03,260 The argument is the text we would like input into the file. 21 00:01:03,260 --> 00:01:06,080 If we use the write method successively, 22 00:01:06,080 --> 00:01:08,670 each time it is called, it will write to the file. 23 00:01:08,670 --> 00:01:10,670 The first time it is called, we will write, 24 00:01:10,670 --> 00:01:14,490 "This is line A " to represent a new line. 25 00:01:14,490 --> 00:01:17,340 The second time we call the method, it will write, 26 00:01:17,340 --> 00:01:20,410 "this is line B " then it will close the file. 27 00:01:20,410 --> 00:01:23,800 We can write each element in a list to a file. 28 00:01:23,800 --> 00:01:28,780 As before, we use a with command and the open function to create a file. 29 00:01:28,780 --> 00:01:33,110 The list, Lines, has three elements consisting of text. 30 00:01:33,110 --> 00:01:35,500 We use a for loop to read each element of 31 00:01:35,500 --> 00:01:38,290 the first lines and pass it to the variable line. 32 00:01:38,290 --> 00:01:41,120 The first iteration of the loop writes 33 00:01:41,120 --> 00:01:44,610 the first element of the list to the file Example2. 34 00:01:44,610 --> 00:01:49,480 The second iteration writes the second element of the list and so on. 35 00:01:49,480 --> 00:01:52,650 At the end of the loop, the file will be closed. 36 00:01:52,650 --> 00:01:56,860 We can set the mode to appended using a lowercase a. 37 00:01:56,860 --> 00:02:01,230 This will not create a new file but just use the existing file. 38 00:02:01,230 --> 00:02:03,500 If we call the method write, 39 00:02:03,500 --> 00:02:05,910 it will just write to the existing file, 40 00:02:05,910 --> 00:02:10,060 then add "This is line C" then close the file. 41 00:02:10,060 --> 00:02:13,760 We can copy one file to a new file as follows. 42 00:02:13,760 --> 00:02:20,540 First, we read the file Example1 and interact with it via the file object, read file. 43 00:02:20,540 --> 00:02:23,720 Then we create a new file Example3 and 44 00:02:23,720 --> 00:02:27,230 use the file object write file to interact with it. 45 00:02:27,230 --> 00:02:30,850 The for loop takes a line from the file object, read file, 46 00:02:30,850 --> 00:02:36,010 and stores it in the file Example3 using the file object, write file. 47 00:02:36,010 --> 00:02:39,100 The first iteration copies the first line. 48 00:02:39,100 --> 00:02:42,070 The second iteration copies the second line, 49 00:02:42,070 --> 00:02:44,130 till the end of the file is reached. 50 00:02:44,130 --> 00:02:46,150 Then both files are closed. 51 00:02:46,150 --> 00:02:49,000 Check out the labs for more examples. 52 00:02:49,000 --> 00:02:54,000 (Music)