1 00:00:00,009 --> 00:00:05,579 Let's now talk about how we can read from and write to text files. 2 00:00:05,880 --> 00:00:07,070 So say, for example, 3 00:00:07,079 --> 00:00:11,229 we wanted to create a program that would accept an input from the user. 4 00:00:11,239 --> 00:00:15,229 Let's say the user has been asked to provide their username 5 00:00:15,659 --> 00:00:21,239 and then we want to store that username on a separate file. How can we do this? Well, 6 00:00:21,520 --> 00:00:25,100 first things first is let us assign a variable 7 00:00:25,110 --> 00:00:27,129 that will accept the input from the user. 8 00:00:27,139 --> 00:00:31,270 So I'm gonna call my variable username equals and now the input function 9 00:00:31,729 --> 00:00:36,479 and now enter your user name, put it straight forward. 10 00:00:36,840 --> 00:00:39,409 Now, remember from the previous lesson are 11 00:00:39,599 --> 00:00:40,650 with open 12 00:00:40,750 --> 00:00:42,400 syntax, I'm gonna say with 13 00:00:42,810 --> 00:00:44,000 and now open 14 00:00:44,319 --> 00:00:46,779 and of course, remember open will, will accept two parameters. 15 00:00:46,790 --> 00:00:49,639 First of all the file you want to work with and then the operator, 16 00:00:50,240 --> 00:00:54,419 I'm gonna create a file called users dot txt. 17 00:00:55,240 --> 00:00:57,139 And then comma 18 00:00:57,500 --> 00:00:59,770 we're going to add the W 19 00:00:59,979 --> 00:01:00,549 function 20 00:01:00,750 --> 00:01:02,549 to, right? And then I'm gonna say 21 00:01:02,740 --> 00:01:03,819 file, 22 00:01:04,699 --> 00:01:05,550 OK? 23 00:01:05,860 --> 00:01:07,760 And now what do we want to do? 24 00:01:07,980 --> 00:01:11,800 We want to write the username to the file. 25 00:01:11,809 --> 00:01:15,540 So what I'll do right now is very straightforward file dot 26 00:01:15,870 --> 00:01:16,959 write. 27 00:01:17,610 --> 00:01:19,110 And now in brackets 28 00:01:19,319 --> 00:01:20,980 user name, 29 00:01:22,199 --> 00:01:23,099 OK? 30 00:01:23,370 --> 00:01:26,379 And then just for good measure, we can print out something 31 00:01:26,589 --> 00:01:30,019 after the user has provided the username. So let's say print 32 00:01:31,669 --> 00:01:32,650 and 33 00:01:32,839 --> 00:01:36,010 I will say a username 34 00:01:36,900 --> 00:01:38,879 has been added 35 00:01:39,000 --> 00:01:40,790 to the file 36 00:01:41,080 --> 00:01:42,639 just as an example. OK? So 37 00:01:42,959 --> 00:01:46,120 let's go ahead right now. Run the program, 38 00:01:46,610 --> 00:01:49,680 enter your username. I'm going to say Alex 39 00:01:49,910 --> 00:01:55,199 press enter and now it says username has been added to the file and I can see users do 40 00:01:55,300 --> 00:01:58,010 text file has been created. And if I open it, 41 00:01:58,279 --> 00:02:00,529 you can see it's right there, Alex 42 00:02:00,680 --> 00:02:01,839 so far. So good. 43 00:02:02,150 --> 00:02:03,080 However, 44 00:02:03,459 --> 00:02:06,839 if I go back and I run the program again 45 00:02:07,459 --> 00:02:12,960 and this time I used a different username, I said Alice, I press enter. 46 00:02:13,479 --> 00:02:15,880 If I go to my user or text file, 47 00:02:16,059 --> 00:02:20,830 you can see right now that Alice has overwritten Alex. 48 00:02:21,039 --> 00:02:24,679 And that's because whenever you're accepting inputs from your users, 49 00:02:24,690 --> 00:02:26,279 your storename in a text file, 50 00:02:26,410 --> 00:02:28,660 when you use the right operator, 51 00:02:28,669 --> 00:02:33,880 it will simply overwrite whatever it is that existed in that file previously. 52 00:02:34,300 --> 00:02:36,679 So what if we don't want this? 53 00:02:36,690 --> 00:02:39,229 What if we just want to keep on adding files 54 00:02:39,240 --> 00:02:42,630 or usernames to our file without overwriting the previous ones? 55 00:02:42,639 --> 00:02:43,779 How do we do this? 56 00:02:44,559 --> 00:02:49,330 First of all, let me clear my history. Remove all users dot Text file. 57 00:02:50,210 --> 00:02:50,949 OK. 58 00:02:51,369 --> 00:02:53,630 So let's pretend like we're starting from scratch 59 00:02:54,330 --> 00:02:56,589 instead of using the W 60 00:02:56,929 --> 00:02:59,289 function, I'm going to use a 61 00:02:59,570 --> 00:03:01,710 which represents a pen. 62 00:03:02,830 --> 00:03:04,220 So with append, 63 00:03:04,419 --> 00:03:07,649 we can keep asking the user to provide a new username. 64 00:03:07,949 --> 00:03:10,669 And when once they keep on adding new usernames, 65 00:03:10,679 --> 00:03:15,350 they will be stored in our file and the previous usernames will not be overwritten. 66 00:03:15,360 --> 00:03:15,690 So 67 00:03:15,960 --> 00:03:17,490 I'm going to run the program again. 68 00:03:18,139 --> 00:03:20,720 Provide my username, Alex. 69 00:03:21,119 --> 00:03:21,600 OK? 70 00:03:21,610 --> 00:03:24,330 And now you can see we have our users dot TXT file, 71 00:03:24,339 --> 00:03:28,059 Alex just like we did with the right operator, right? 72 00:03:28,070 --> 00:03:29,479 But now if I go back, 73 00:03:30,199 --> 00:03:32,179 I run the program again. 74 00:03:32,369 --> 00:03:34,649 And this time I say Alice, 75 00:03:35,710 --> 00:03:37,830 what now happens? I go to my users dot text 76 00:03:38,070 --> 00:03:41,690 file and there you go. We have Alex, we have Alice. 77 00:03:42,020 --> 00:03:44,050 So you can see Alex was not overwritten. 78 00:03:44,160 --> 00:03:45,320 And by the way, 79 00:03:46,050 --> 00:03:49,199 if you wanted to store your usernames in separate lines, 80 00:03:49,350 --> 00:03:53,350 all we need to do right here is where we write in the file, username. 81 00:03:53,440 --> 00:03:55,559 We can just say plus, 82 00:03:55,660 --> 00:03:56,750 OK? And now 83 00:03:56,899 --> 00:03:57,869 check this out, 84 00:03:58,139 --> 00:03:59,229 you wanna say 85 00:03:59,600 --> 00:04:00,820 forward slash, 86 00:04:01,160 --> 00:04:03,229 I'm sorry, backward slash. And then N 87 00:04:03,880 --> 00:04:06,539 so this right here, this is what we use to 88 00:04:06,820 --> 00:04:08,960 create our lines. OK? So 89 00:04:09,100 --> 00:04:12,330 whatever you're adding will be added onto a new line. 90 00:04:12,619 --> 00:04:13,220 So 91 00:04:13,350 --> 00:04:16,100 what I'm gonna do here is let us remove 92 00:04:16,500 --> 00:04:17,399 the users dot text 93 00:04:17,519 --> 00:04:18,700 file again. 94 00:04:19,510 --> 00:04:20,809 Let me run the program 95 00:04:21,250 --> 00:04:22,209 one more time, 96 00:04:22,619 --> 00:04:24,890 Alex press enter, 97 00:04:25,100 --> 00:04:26,869 let's run the program again. 98 00:04:27,200 --> 00:04:29,489 And this time I'm gonna say Alice 99 00:04:30,459 --> 00:04:31,640 press enter 100 00:04:31,980 --> 00:04:34,690 and now if I open up my users do text file 101 00:04:34,700 --> 00:04:39,399 now you can see Alex and Alice are on separate lines. 102 00:04:39,609 --> 00:04:42,200 So this right here is the difference between 103 00:04:42,910 --> 00:04:46,750 your light operator and your append operator. 104 00:04:46,989 --> 00:04:49,559 But what if we wanted to read 105 00:04:49,859 --> 00:04:55,290 the data, the usernames from the file? How can we do it as well? 106 00:04:56,250 --> 00:05:00,309 I'm just gonna come down in here, say with open again 107 00:05:00,989 --> 00:05:01,850 brackets. 108 00:05:02,640 --> 00:05:05,910 And now I'm gonna say users dot TXT 109 00:05:06,579 --> 00:05:07,859 and then what are we gonna do? 110 00:05:08,140 --> 00:05:09,649 Codes are? 111 00:05:09,910 --> 00:05:12,109 OK? And then as file 112 00:05:12,739 --> 00:05:13,540 call on 113 00:05:13,989 --> 00:05:14,880 and now 114 00:05:15,660 --> 00:05:19,559 we want to create a for loop. So I'm gonna say for 115 00:05:19,859 --> 00:05:21,200 name, 116 00:05:21,690 --> 00:05:24,640 OK? For name in file. 117 00:05:24,809 --> 00:05:29,019 So for the usernames, for the names in the file, what do we want to do? 118 00:05:29,239 --> 00:05:32,709 We want to print, I'm gonna say print 119 00:05:33,260 --> 00:05:35,209 and then we can say, OK, 120 00:05:35,660 --> 00:05:37,160 uh username 121 00:05:39,970 --> 00:05:41,390 colon and now 122 00:05:42,589 --> 00:05:43,350 add a 123 00:05:43,649 --> 00:05:44,100 comma 124 00:05:44,730 --> 00:05:46,299 and now name 125 00:05:46,559 --> 00:05:47,380 dot 126 00:05:48,010 --> 00:05:49,630 strip, 127 00:05:50,440 --> 00:05:53,260 this is a new function and it's going to 128 00:05:53,420 --> 00:05:54,399 have its own 129 00:05:54,589 --> 00:05:55,899 empty brackets 130 00:05:56,230 --> 00:05:56,779 and 131 00:05:57,029 --> 00:06:00,619 there it is. So now if I run the program 132 00:06:01,839 --> 00:06:03,059 Alex 133 00:06:03,519 --> 00:06:06,220 and there you go, 134 00:06:06,450 --> 00:06:08,390 using them has been edited to file. 135 00:06:08,519 --> 00:06:11,339 And now because of our function over here 136 00:06:11,980 --> 00:06:13,869 where we read the file, 137 00:06:14,179 --> 00:06:15,850 it's gonna say username, 138 00:06:16,089 --> 00:06:17,769 Alex, username, 139 00:06:17,899 --> 00:06:22,200 Alice, and then username Alex again because the user dot text file has Alex 140 00:06:22,320 --> 00:06:22,489 Alice, 141 00:06:22,649 --> 00:06:23,420 Alex. 142 00:06:23,640 --> 00:06:26,880 So this is how we can read 143 00:06:27,160 --> 00:06:29,559 the contents of our file. 144 00:06:29,570 --> 00:06:33,019 And by the way, the dot strip right here, 145 00:06:33,100 --> 00:06:38,010 this is a method that's very often used whenever we are handling files. 146 00:06:38,070 --> 00:06:43,220 Now we use it to remove any sort of like leading or trailing white space characters 147 00:06:43,329 --> 00:06:44,239 from a string. 148 00:06:44,250 --> 00:06:49,149 And this may include things like your spaces tabs or even new line characters. 149 00:06:49,160 --> 00:06:53,309 So it's just a very efficient way of ensuring that whatever text we are 150 00:06:53,600 --> 00:06:55,269 outputting or printing out 151 00:06:55,420 --> 00:06:59,910 it is in the most efficient sort of manner without any unnecessary space. 152 00:06:59,920 --> 00:07:02,019 That's why dot Strip is very often used 153 00:07:02,369 --> 00:07:05,070 whenever we're handling text files. 154 00:07:05,170 --> 00:07:09,850 Now it is completely optional. You don't have to use the strip method. In fact, 155 00:07:10,029 --> 00:07:11,260 if I removed 156 00:07:12,119 --> 00:07:13,260 the strip method 157 00:07:13,579 --> 00:07:15,989 and just close my bracket, normally, 158 00:07:16,220 --> 00:07:17,570 if I run the program, 159 00:07:17,859 --> 00:07:23,299 it will work the exact same way. So let me provide a new name. Let's let's say Mandy 160 00:07:23,769 --> 00:07:28,170 as an example. And there you go, we have username Alice, user name, Alex name Mandy 161 00:07:28,339 --> 00:07:30,070 and there it is, it's still gonna work. 162 00:07:30,079 --> 00:07:35,089 But you can see right now that we do have the extra space, the lines between 163 00:07:35,279 --> 00:07:37,910 uh username, Alice, username, Alex, 164 00:07:37,920 --> 00:07:44,600 username Mandy because we are using the new line character, the new line string here 165 00:07:44,779 --> 00:07:48,109 to add each input on a separate line. 166 00:07:48,420 --> 00:07:52,269 So that's pretty much it for reading and writing to text files. 167 00:07:52,279 --> 00:07:54,519 Thank you for watching. I will see you in the next class.