WEBVTT 1 00:00:00.170 --> 00:00:02.020 line:15% Hi guys, and welcome back. 2 00:00:02.020 --> 00:00:03.100 In this video, we're going to be 3 00:00:03.100 --> 00:00:05.270 working with opening files, 4 00:00:05.270 --> 00:00:07.200 and it's going to be really easy, 5 00:00:07.200 --> 00:00:08.790 because all we have to do 6 00:00:08.790 --> 00:00:10.910 is ask the user which file they wanna open 7 00:00:10.910 --> 00:00:12.010 using another dialogue 8 00:00:12.010 --> 00:00:13.720 like the one we've seen already, 9 00:00:13.720 --> 00:00:14.900 get the contents, 10 00:00:14.900 --> 00:00:16.550 and then create a new file 11 00:00:16.550 --> 00:00:18.320 and put those contents in it. 12 00:00:18.320 --> 00:00:22.230 Let's get started by creating our open file. 13 00:00:22.230 --> 00:00:24.423 So, we'll do open_file() here, 14 00:00:25.430 --> 00:00:29.088 and then we're gonna ask for a file_path(), 15 00:00:29.088 --> 00:00:32.050 so we say file_path() equal filedialog, 16 00:00:32.050 --> 00:00:34.667 and now we're gonna say askopenfilename. 17 00:00:37.130 --> 00:00:38.960 Then, we will do as we did before. 18 00:00:38.960 --> 00:00:40.720 We will extract the file name 19 00:00:40.720 --> 00:00:42.680 from the path that this gives you. 20 00:00:42.680 --> 00:00:44.653 So, we will do filename() equal 21 00:00:44.653 --> 00:00:47.267 os.path.basename() file_path. 22 00:00:49.530 --> 00:00:51.170 Next up, we're gonna open the file 23 00:00:51.170 --> 00:00:53.820 with open() file_path. 24 00:00:53.820 --> 00:00:55.973 and "r" as file. 25 00:00:57.220 --> 00:00:58.710 We will then 26 00:00:58.710 --> 00:00:59.550 get the content, 27 00:00:59.550 --> 00:01:00.700 which is file.read, 28 00:01:00.700 --> 00:01:02.980 and this reads the entire file. 29 00:01:02.980 --> 00:01:05.180 Now if you have extremely long files, 30 00:01:05.180 --> 00:01:07.130 this could present a bit of a bottleneck, 31 00:01:07.130 --> 00:01:07.963 but we're not gonna worry 32 00:01:07.963 --> 00:01:09.233 about that in this course. 33 00:01:11.460 --> 00:01:12.700 Then we will have an except, 34 00:01:12.700 --> 00:01:13.533 as usual, 35 00:01:13.533 --> 00:01:16.130 for AttributeError and FileNotFoundError, 36 00:01:16.130 --> 00:01:18.750 so in case that something does fail, 37 00:01:18.750 --> 00:01:20.500 or we close the 38 00:01:20.500 --> 00:01:22.110 dialogue, 39 00:01:22.110 --> 00:01:22.943 then we will say 40 00:01:22.943 --> 00:01:25.820 "print Open operation cancelled" 41 00:01:25.820 --> 00:01:28.100 and we will return so that we don't execute 42 00:01:28.100 --> 00:01:29.660 this next piece of code, 43 00:01:29.660 --> 00:01:31.440 which is create_file(), 44 00:01:31.440 --> 00:01:33.490 but now we're gonna pass in the content 45 00:01:33.490 --> 00:01:34.590 and the filename 46 00:01:34.590 --> 00:01:36.560 into create_file(). 47 00:01:36.560 --> 00:01:38.635 Let's go back up here to create_file(), 48 00:01:38.635 --> 00:01:41.360 and we will have a content of empty string, 49 00:01:41.360 --> 00:01:44.970 and a title of Untitled. 50 00:01:44.970 --> 00:01:48.710 Then, here we will replace Text equal Untitled 51 00:01:48.710 --> 00:01:49.560 by 52 00:01:49.560 --> 00:01:50.980 title 53 00:01:50.980 --> 00:01:51.860 and 54 00:01:51.860 --> 00:01:54.360 we're also going to insert 55 00:01:54.360 --> 00:01:56.530 the text in a text area. 56 00:01:56.530 --> 00:01:59.950 What we'll do is a text_area.insert, 57 00:01:59.950 --> 00:02:02.380 and where do we wanna insert the text, 58 00:02:02.380 --> 00:02:03.550 that is the first thing that 59 00:02:03.550 --> 00:02:05.130 this method requires, 60 00:02:05.130 --> 00:02:07.830 and we're gonna use the special place end, 61 00:02:07.830 --> 00:02:09.770 which will insert the text at the end 62 00:02:09.770 --> 00:02:11.650 or at the very last character 63 00:02:11.650 --> 00:02:13.470 currently in the text area. 64 00:02:13.470 --> 00:02:15.100 Because this text area is empty, 65 00:02:15.100 --> 00:02:16.100 we just created it, 66 00:02:16.100 --> 00:02:17.230 this is going to essentially 67 00:02:17.230 --> 00:02:18.593 be the start as well. 68 00:02:19.450 --> 00:02:22.923 And, then the content that we wanna insert. 69 00:02:24.810 --> 00:02:26.820 All right, so this how you open 70 00:02:26.820 --> 00:02:27.690 a file 71 00:02:27.690 --> 00:02:28.530 using 72 00:02:28.530 --> 00:02:29.520 our editor, 73 00:02:29.520 --> 00:02:30.960 very straight forward 74 00:02:30.960 --> 00:02:32.060 once again 75 00:02:32.060 --> 00:02:33.650 ask for the file, 76 00:02:33.650 --> 00:02:34.510 get the file name 77 00:02:34.510 --> 00:02:37.480 so that you can then name the tab that, 78 00:02:37.480 --> 00:02:39.260 read the file contents, 79 00:02:39.260 --> 00:02:40.870 and then create the file, 80 00:02:40.870 --> 00:02:43.780 making sure to pass in the contents of the file 81 00:02:43.780 --> 00:02:45.250 and the title. 82 00:02:45.250 --> 00:02:47.390 By default, when we create a file 83 00:02:47.390 --> 00:02:49.833 such as what we do down here, 84 00:02:50.810 --> 00:02:51.680 we want 85 00:02:52.640 --> 00:02:54.140 the content and the title 86 00:02:54.140 --> 00:02:55.810 to be these default values, 87 00:02:55.810 --> 00:02:58.130 So empty content and Untitled, 88 00:02:58.130 --> 00:02:59.760 that's why we're making sure 89 00:02:59.760 --> 00:03:00.850 that these have defaults 90 00:03:00.850 --> 00:03:03.850 so that when you do insert the content, 91 00:03:03.850 --> 00:03:05.260 it is gonna be empty, 92 00:03:05.260 --> 00:03:06.590 and when you do add the tab, 93 00:03:06.590 --> 00:03:07.960 it is gonna be Untitled, 94 00:03:07.960 --> 00:03:09.920 unless we specify otherwise. 95 00:03:09.920 --> 00:03:11.520 Finally, the last thing that we want to add 96 00:03:11.520 --> 00:03:14.540 is of course a way to call this function, 97 00:03:14.540 --> 00:03:16.550 as well as new and save, 98 00:03:16.550 --> 00:03:18.330 we wanna add in open. 99 00:03:18.330 --> 00:03:22.380 So we will do file_menu.add_command(), 100 00:03:22.380 --> 00:03:24.680 label is going to be Open..., 101 00:03:24.680 --> 00:03:26.050 as is usual, 102 00:03:26.050 --> 00:03:28.070 and the command is gonna be open_file(), 103 00:03:28.070 --> 00:03:30.270 making sure to not call it. 104 00:03:30.270 --> 00:03:31.283 Let's run the app. 105 00:03:32.280 --> 00:03:33.510 And now we're gonna open 106 00:03:36.690 --> 00:03:40.180 this file called test.txt 107 00:03:40.180 --> 00:03:41.670 that we made earlier, 108 00:03:41.670 --> 00:03:44.700 and there you have test.txt. 109 00:03:44.700 --> 00:03:46.390 Now, notice that you have still an 110 00:03:46.390 --> 00:03:48.740 untitled tab at the start. 111 00:03:48.740 --> 00:03:49.690 This is a problem that we're 112 00:03:49.690 --> 00:03:50.730 not gonna concern ourselves 113 00:03:50.730 --> 00:03:52.150 too much with right now, 114 00:03:52.150 --> 00:03:54.618 but you could check to see if the tab 115 00:03:54.618 --> 00:03:56.200 that is currently selected when 116 00:03:56.200 --> 00:03:57.780 you open a new one 117 00:03:57.780 --> 00:03:59.320 is empty, 118 00:03:59.320 --> 00:04:00.350 and if it is, 119 00:04:00.350 --> 00:04:01.900 then you can possibly close it, 120 00:04:01.900 --> 00:04:04.390 and instead create this new one. 121 00:04:04.390 --> 00:04:05.223 But, as I said, 122 00:04:05.223 --> 00:04:06.980 we're not gonna do that in this course, 123 00:04:06.980 --> 00:04:08.690 just a separate problem 124 00:04:08.690 --> 00:04:10.853 that you can fix if you'd like. 125 00:04:11.700 --> 00:04:13.170 Thanks for joining me in this video. 126 00:04:13.170 --> 00:04:14.230 I hope you've learned something, 127 00:04:14.230 --> 00:04:15.880 line:15% and I'll see you in the next one.