1 00:00:00,000 --> 00:00:07,900 [no audio] 2 00:00:07,900 --> 00:00:12,900 Friends, using Python you can work with like text files, JSON 3 00:00:12,900 --> 00:00:19,300 files, CSV files, HTML files, and so on, and as of now here we 4 00:00:19,300 --> 00:00:22,000 are going to work with text files. 5 00:00:22,100 --> 00:00:24,900 So simply working with text files using Python. 6 00:00:25,700 --> 00:00:30,300 So guys before going to work with your text files using Python 7 00:00:30,800 --> 00:00:34,800 let's move into our general GUI procedure whenever if 8 00:00:34,800 --> 00:00:39,200 you want to create or read the content from your text file, 9 00:00:39,200 --> 00:00:44,400 right. See what I am doing is, suppose I want to create a 10 00:00:44,400 --> 00:00:46,100 new file, right, 11 00:00:46,200 --> 00:00:49,000 yeah, before that see guys, generally if you want to work with 12 00:00:49,000 --> 00:00:51,100 text files, right, you have three options. 13 00:00:51,100 --> 00:00:53,500 That is you can create a new file, 14 00:00:54,700 --> 00:01:03,300 or add content to an existing file, or just for reading purpose 15 00:01:03,300 --> 00:01:05,200 read a file content. 16 00:01:06,800 --> 00:01:11,400 Right. See, whenever if you are going with GUI procedure, 17 00:01:11,400 --> 00:01:17,500 I mean in this way, 'Right Click', 'New', then your file name, 18 00:01:17,600 --> 00:01:20,800 I mean, let me select my file as text file. 19 00:01:20,800 --> 00:01:22,800 I mean, I want to create file as text. 20 00:01:24,100 --> 00:01:25,100 Right. I'm creating, 21 00:01:25,100 --> 00:01:29,900 let's say file name as some 'demo.txt', right. Now 22 00:01:29,900 --> 00:01:31,600 I am going to open it. Observe that. 23 00:01:31,600 --> 00:01:33,600 I am going to open it. Open it. 24 00:01:34,200 --> 00:01:39,100 Yes, I opened. Right. See guys, in your GUI procedure 25 00:01:39,600 --> 00:01:42,900 whenever if you open your file, you can read your file, 26 00:01:43,800 --> 00:01:48,400 you can write content to your file, right, all the options 27 00:01:48,400 --> 00:01:52,400 are available simply by opening your file in GUI mode, but 28 00:01:52,600 --> 00:01:55,400 command line, or through programming, or through scripting, 29 00:01:55,500 --> 00:01:56,500 it's not possible. 30 00:01:56,800 --> 00:02:00,300 You have to open your file either to write content, or to 31 00:02:00,300 --> 00:02:05,000 read content, or to add some new data to the existing file. 32 00:02:06,500 --> 00:02:10,400 Be clear. See in GUI mode it is possible simply by opening your 33 00:02:10,400 --> 00:02:13,900 file, but whereas through programming if you want to work 34 00:02:13,900 --> 00:02:17,500 with your files, right, you have to open your file, 35 00:02:17,500 --> 00:02:22,200 you have to open your file either in 'write' mode, means if 36 00:02:22,200 --> 00:02:26,600 you want to create a new file, right, or you have to open 37 00:02:26,600 --> 00:02:31,500 your file in 'append' mode, means adding new content to existing 38 00:02:31,500 --> 00:02:35,800 file, then you have last option, that is 'read' mode, just to 39 00:02:35,800 --> 00:02:40,600 read. Right. See that. You can open your file - 40 00:02:41,000 --> 00:02:42,400 If you want to create a new file 41 00:02:42,400 --> 00:02:44,300 you have to open your file with the 'w' mode, 42 00:02:45,400 --> 00:02:48,800 or if you have already a file and if you want to add, append 43 00:02:48,800 --> 00:02:51,800 data to an existing file, then you have to open your file 44 00:02:51,800 --> 00:02:52,800 in 'append' mode, 45 00:02:53,800 --> 00:02:56,800 or simply if you want to read content from your file, you 46 00:02:56,800 --> 00:03:01,100 have to open it using 'r' mode. Right. Fine. 47 00:03:01,200 --> 00:03:05,300 And one more thing guys, once if you write some content to your 48 00:03:05,300 --> 00:03:08,400 file, right, see I'm opening, steps are important, 49 00:03:08,400 --> 00:03:11,700 I am opening, based on cursor position 50 00:03:11,800 --> 00:03:14,800 you are going to write content. Suppose if cursor is somewhere 51 00:03:14,800 --> 00:03:18,200 there, I am moving cursor to some place, then I can write. 52 00:03:18,800 --> 00:03:21,600 So wherever your cursor is there, based on that cursor only 53 00:03:21,600 --> 00:03:23,400 you are writing content to your file. 54 00:03:23,400 --> 00:03:26,600 [no audio] 55 00:03:26,600 --> 00:03:31,500 Fine. So assume that I am writing, "This is a demo file", 56 00:03:32,800 --> 00:03:35,500 and I am going to close it, while closing it is asking 'Save' 57 00:03:35,500 --> 00:03:39,600 option. But in programming in your Python Script, once if you 58 00:03:39,600 --> 00:03:43,600 close, automatically the data will save into your file. 59 00:03:45,100 --> 00:03:48,400 You don't need to provide confirmation like save or don't 60 00:03:48,400 --> 00:03:52,500 save. Whenever if you close, automatically your data will store 61 00:03:52,500 --> 00:03:55,500 into your file. Right. See now 62 00:03:55,500 --> 00:03:57,500 [no audio] 63 00:03:57,500 --> 00:04:01,700 I'm going to, see guys, whether you want to read a content, 64 00:04:01,800 --> 00:04:05,400 or whether you want to write a content to your file always you 65 00:04:05,400 --> 00:04:09,600 should get in your programming first your cursor position, 66 00:04:10,100 --> 00:04:11,700 or your file object. 67 00:04:13,000 --> 00:04:14,100 You can take any variable. 68 00:04:14,100 --> 00:04:17,399 I am taking 'fo', file object. Just assume that this is like 69 00:04:17,399 --> 00:04:18,600 your cursor position. 70 00:04:18,800 --> 00:04:21,800 So once if you get into your hand, cursor position, then after 71 00:04:21,800 --> 00:04:24,500 that whether you want to read or write you can do it. 72 00:04:25,200 --> 00:04:31,600 So now I want to get my cursor, right, to write content to 73 00:04:31,600 --> 00:04:34,800 your file. Then open your file in 'write' mode. 74 00:04:35,900 --> 00:04:37,500 Open your file in 'write' mode. 75 00:04:37,700 --> 00:04:40,400 Let's say 'newfile.txt'. 76 00:04:40,400 --> 00:04:44,300 I'm writing, then 'w' mode, 'write' mode. 77 00:04:45,300 --> 00:04:47,700 Then after that simply you can close. 78 00:04:48,400 --> 00:04:51,200 So as of now, what I am doing is simply I'm opening a file 79 00:04:51,600 --> 00:04:54,400 and then closing a file. While opening, right, 80 00:04:54,700 --> 00:04:58,000 I'm giving some file name. Automatically this file will be created 81 00:04:58,200 --> 00:04:59,400 with empty data. 82 00:05:00,600 --> 00:05:02,900 Now this is your cursor position. With cursor position 83 00:05:02,900 --> 00:05:04,800 nowhere I am writing any data to your file. 84 00:05:04,800 --> 00:05:05,900 Simply I'm closing. 85 00:05:06,000 --> 00:05:08,900 It's like creating an empty file, right? 86 00:05:09,200 --> 00:05:10,200 Let me check it. 87 00:05:10,200 --> 00:05:11,200 So this is simply 88 00:05:11,200 --> 00:05:14,500 [no audio] 89 00:05:14,500 --> 00:05:17,200 'Creation of empty file'. That's it. 90 00:05:17,600 --> 00:05:19,200 Let me open editor. 91 00:05:19,400 --> 00:05:24,200 So I am going to open my, you can take a Sublime or Atom editor, 92 00:05:24,400 --> 00:05:28,700 whatever it maybe you can take. I'm going to suppose, Sublime Text. 93 00:05:29,200 --> 00:05:30,700 Right. Let's open that first. 94 00:05:30,700 --> 00:05:34,900 [no audio] 95 00:05:34,900 --> 00:05:36,100 Let me create a file. 96 00:05:36,100 --> 00:05:37,300 So guys, first of all, 97 00:05:38,400 --> 00:05:39,900 right, so, "Working with 98 00:05:39,900 --> 00:05:41,800 [no audio] 99 00:05:41,800 --> 00:05:43,700 text files". You know to work with your 100 00:05:43,700 --> 00:05:47,500 text files you have different modes - write, read or append. 101 00:05:47,500 --> 00:05:50,300 First of all because we are going to create a new file, 102 00:05:50,300 --> 00:05:52,200 assume that we don't have any files to read, 103 00:05:52,400 --> 00:05:55,100 we need to create some file, right? 104 00:05:55,100 --> 00:05:57,700 So before going to create always in your hand you should 105 00:05:57,700 --> 00:05:59,900 have a cursor position from your file. 106 00:05:59,900 --> 00:06:02,800 That's why what I am doing is, first I'm going to open my 107 00:06:02,800 --> 00:06:08,400 file. Let's say some file, right, some 'newdemo.txt' file 108 00:06:08,400 --> 00:06:11,700 I am creating. So I want to open this in 'write' mode. 109 00:06:11,800 --> 00:06:15,500 So now this file I am opening for 'write' mode. 110 00:06:16,000 --> 00:06:20,000 So once you've opened your file automatically this syntax is going 111 00:06:20,000 --> 00:06:24,100 to give your cursor object, simply file object. That's it. 112 00:06:25,100 --> 00:06:26,600 Now based on this object 113 00:06:26,600 --> 00:06:28,400 you can write some content to your file. 114 00:06:28,400 --> 00:06:31,800 But what I am doing is, simply I opened my file, and then closed. 115 00:06:33,100 --> 00:06:36,300 Right. Let me save it. Be clear. 116 00:06:36,300 --> 00:06:38,200 [no audio] 117 00:06:38,200 --> 00:06:40,600 Let me save it with some name, right. 118 00:06:40,600 --> 00:06:42,700 So I'm going to store on my Desktop. 119 00:06:42,700 --> 00:06:48,300 [no audio] 120 00:06:48,300 --> 00:06:50,700 'create_a_file.py'. That's it. 121 00:06:51,700 --> 00:06:53,100 Now this is a Python Script. 122 00:06:53,200 --> 00:06:56,700 Now, what I am doing is, I am going to run our code. Just simply 123 00:06:56,900 --> 00:06:57,900 'Ctrl + B' 124 00:06:57,900 --> 00:06:59,900 [no audio] 125 00:06:59,900 --> 00:07:01,500 Done. Done. 126 00:07:02,000 --> 00:07:04,700 You don't have any output here because you don't have any 127 00:07:04,700 --> 00:07:06,500 'print' statement to get your output, right. 128 00:07:06,900 --> 00:07:08,200 Simply we created a file. 129 00:07:08,800 --> 00:07:10,300 Let me open our folder. 130 00:07:10,300 --> 00:07:16,000 [no audio] 131 00:07:16,000 --> 00:07:19,400 'create_a_file', just now we created this. Let me, sorry, 132 00:07:19,400 --> 00:07:21,500 that is a Python Script, 133 00:07:21,500 --> 00:07:24,200 right. So our file name is 'newdemo'. 134 00:07:24,200 --> 00:07:27,900 [no audio] 135 00:07:27,900 --> 00:07:32,200 Yeah, see here, 'newdemo.txt'. Open this. You don't have 136 00:07:32,200 --> 00:07:37,400 any content because you just opened and closed your file, okay. 137 00:07:37,800 --> 00:07:40,200 Okay, we'll write our data to your file, 138 00:07:40,200 --> 00:07:44,100 but before that suppose I want to see in which mode your 139 00:07:44,100 --> 00:07:45,500 file is opened. 140 00:07:45,900 --> 00:07:48,500 You can check it simply 'print(fo.mode)'. 141 00:07:48,500 --> 00:07:53,000 So it is going to give you the mode of your file 'write' 142 00:07:53,000 --> 00:07:55,900 mode. Right. Then, 143 00:07:57,000 --> 00:07:58,900 let me comment this. Now, 144 00:08:00,100 --> 00:08:03,200 you can check it, 'fo.readable'. 145 00:08:04,800 --> 00:08:08,700 Observe the output, what you are getting. Yeah, this is a function. 146 00:08:09,700 --> 00:08:11,800 Let me run it, 'False' 147 00:08:11,800 --> 00:08:16,500 you are getting. That means you opened your file not for read, 148 00:08:16,500 --> 00:08:20,700 some other thing, maybe write mode or append mode. That we 149 00:08:20,700 --> 00:08:22,600 have seen by using 'fo.mode'. 150 00:08:23,600 --> 00:08:27,200 So 'fo.readable', it is going to give 'False' means you 151 00:08:27,200 --> 00:08:32,700 can't read the content if you open your file with 'w' mode, that's it. 152 00:08:34,200 --> 00:08:36,900 Let me print one more thing, 'fo. 153 00:08:38,000 --> 00:08:40,799 writable'. Yes, 'True' 154 00:08:40,799 --> 00:08:42,100 you're getting. Yes, 155 00:08:42,100 --> 00:08:43,700 we can write some content to this file. 156 00:08:43,900 --> 00:08:45,100 Actually, no need to check this. 157 00:08:45,200 --> 00:08:46,200 Yes, there are some options 158 00:08:46,200 --> 00:08:48,900 that's why I am giving this. Right. 159 00:08:48,900 --> 00:08:50,799 Now, let me comment this entire code. 160 00:08:50,799 --> 00:08:54,100 [no audio] 161 00:08:54,100 --> 00:08:55,299 Be clear. Now, 162 00:08:55,299 --> 00:08:58,200 what I am doing guys? I want to write a file. Then first open 163 00:08:58,200 --> 00:08:59,400 with some name, random name, 164 00:08:59,400 --> 00:09:00,800 you can take anything, right? 165 00:09:00,800 --> 00:09:02,800 I am taking suppose 'random.txt'. 166 00:09:03,500 --> 00:09:06,000 So you want to write to a file at very first time, you don't 167 00:09:06,000 --> 00:09:08,900 have any file previously, assume that. Then open it in 'write' 168 00:09:08,900 --> 00:09:11,900 mode. So whenever if you open with this syntax automatically 169 00:09:11,900 --> 00:09:13,900 your Python will give your file object. 170 00:09:13,900 --> 00:09:16,200 Assume that there is like a cursor 171 00:09:17,500 --> 00:09:18,800 in your Notepad. 172 00:09:19,300 --> 00:09:24,000 Yes, now in your hand you have a cursor position, then what I want 173 00:09:24,000 --> 00:09:27,300 to do is I want to write some content. Then based on your 174 00:09:27,300 --> 00:09:29,700 cursor position just '.write' operation. 175 00:09:29,700 --> 00:09:34,500 Let me write, "This is a first line", I am writing. 176 00:09:35,600 --> 00:09:37,100 Then after that close it. 177 00:09:38,500 --> 00:09:41,700 So guys what we did is just we have written three lines of 178 00:09:41,700 --> 00:09:44,700 code to create a file and to write some content into that 179 00:09:44,700 --> 00:09:49,200 file. Now if you run this code then only your file will create. 180 00:09:49,700 --> 00:09:52,300 You have written a Python Script just to create your file 181 00:09:52,300 --> 00:09:56,500 with content of, "This is a first line". 182 00:09:57,700 --> 00:10:01,700 Right. Let me save it and run it. It's done. 183 00:10:02,300 --> 00:10:05,500 Right. Let me go to here. Somewhere 184 00:10:05,500 --> 00:10:06,700 you will have 'random'. See that. 185 00:10:06,700 --> 00:10:07,800 Let me open this file. 186 00:10:08,300 --> 00:10:10,300 "This is a first line". 187 00:10:11,500 --> 00:10:13,700 Guys, what I am doing is, 188 00:10:14,700 --> 00:10:17,000 I'm going to write one more line. 'write(" 189 00:10:18,100 --> 00:10:20,800 This is a second line"). 190 00:10:21,500 --> 00:10:23,100 Let me save it and run it. 191 00:10:23,100 --> 00:10:25,700 [no audio] 192 00:10:25,700 --> 00:10:27,400 Open your file and see the content. 193 00:10:28,900 --> 00:10:30,300 "This is a first line". 194 00:10:30,500 --> 00:10:31,500 "This is a second line". 195 00:10:31,500 --> 00:10:35,200 You're getting both the lines in the same line in your file. 196 00:10:35,400 --> 00:10:37,200 But actually that is not our requirement. 197 00:10:37,300 --> 00:10:39,500 We need to get, "This is a first line" in one line, 198 00:10:39,500 --> 00:10:43,200 "This is a second line" in next line. But whenever if you want 199 00:10:43,200 --> 00:10:48,900 to, whenever if you want to send the next line into next line 200 00:10:50,200 --> 00:10:52,800 at the end of each and every line you have to write '\n' 201 00:10:52,800 --> 00:10:54,800 so that your cursor will go to next line. 202 00:10:54,800 --> 00:10:58,600 I mean generally you know, suppose assume that you are going 203 00:10:58,600 --> 00:11:00,600 to write some content in any file, right, 204 00:11:00,600 --> 00:11:03,100 so what I'm doing is manually I'm typing, 205 00:11:03,100 --> 00:11:05,100 [no audio] 206 00:11:05,100 --> 00:11:06,600 "This is a line". 207 00:11:06,600 --> 00:11:08,900 Guys, if I want to write content to next line what I am doing, 208 00:11:09,000 --> 00:11:13,400 I am entering my 'Enter' key on my keyboard, right, then only 209 00:11:13,400 --> 00:11:14,800 your cursor is going to next line. 210 00:11:15,200 --> 00:11:20,200 That's what I am doing here with help of '\n'. For last line, 211 00:11:20,200 --> 00:11:22,600 suppose if this is last line you don't need to send your cursor 212 00:11:22,600 --> 00:11:23,700 to the next line. That's it. 213 00:11:24,000 --> 00:11:28,900 Let me save it and run it. So before run let me close this 214 00:11:28,900 --> 00:11:32,200 file. Yeah, let me run it. 215 00:11:33,200 --> 00:11:38,100 Now open your file, check the content. Now you have, "This is 216 00:11:38,100 --> 00:11:39,300 a first line" in one line. 217 00:11:39,300 --> 00:11:42,700 "This is a second line" in next line, right. 218 00:11:42,700 --> 00:11:45,600 So automatically your cursor is sending to next line. Suppose 219 00:11:45,600 --> 00:11:51,700 if I want to write one more line, 'write(, then, "This is a third 220 00:11:51,700 --> 00:11:55,500 line")'. I am writing. So guys not only here, before writing your 221 00:11:55,500 --> 00:11:58,500 third line also you can send your cursor to the next line. '\n' means 222 00:11:58,500 --> 00:12:00,900 simply your cursor will go to next line. In next line 223 00:12:00,900 --> 00:12:03,200 this content will write but providing '\n' 224 00:12:03,200 --> 00:12:05,600 at very first line is not a good practice, 225 00:12:05,600 --> 00:12:09,300 that's why always we mention at the end of your content 226 00:12:09,300 --> 00:12:12,300 '\n'. You need to remember this one. 227 00:12:13,300 --> 00:12:14,700 Let me save it and run it. 228 00:12:14,700 --> 00:12:17,100 [no audio] 229 00:12:17,100 --> 00:12:19,900 Sorry, where is your file? Here, 'random'. Open it and see the 230 00:12:19,900 --> 00:12:23,000 content. You have three lines. And one more thing guys, 231 00:12:23,200 --> 00:12:24,400 it's very important. 232 00:12:24,700 --> 00:12:25,900 I am running this code again 233 00:12:25,900 --> 00:12:26,800 and again. Let me run it 234 00:12:26,800 --> 00:12:32,500 once, I run second time, third time. Three times I run this 235 00:12:32,500 --> 00:12:36,400 script. But if you observe your content, right, if you observe 236 00:12:36,400 --> 00:12:41,500 your content, it is only three lines. So you can feel, I mean 237 00:12:42,400 --> 00:12:46,100 you are running your code three times then maybe your data 238 00:12:46,100 --> 00:12:47,200 will be there three times 239 00:12:47,200 --> 00:12:50,300 in your file, but it is not there in your file 240 00:12:50,300 --> 00:12:51,000 three times, it 241 00:12:51,200 --> 00:12:55,400 is there only one time. See you are not getting like this, 242 00:12:55,400 --> 00:12:57,100 no, you're running your program three times, 243 00:12:57,100 --> 00:13:00,400 then you're not getting your file in this way. 244 00:13:01,100 --> 00:13:04,100 Your data is not repeating three times in your file. 245 00:13:04,600 --> 00:13:05,700 It won't happen. 246 00:13:06,300 --> 00:13:09,000 See what actually it is doing, whenever if you open your file 247 00:13:09,000 --> 00:13:10,000 in write mode, 248 00:13:10,000 --> 00:13:11,100 it's very important, 249 00:13:12,100 --> 00:13:16,300 whenever if you open your file in write mode, if file is already 250 00:13:16,300 --> 00:13:19,500 there, then first of all your Python will simply remove 251 00:13:19,500 --> 00:13:23,400 this file and then same file name will be created. 252 00:13:25,000 --> 00:13:30,300 If file is not there, new file is going to be created, or if file is not there 253 00:13:30,300 --> 00:13:33,300 it will create a new file, if file is already there your content 254 00:13:33,300 --> 00:13:38,100 we will overwrite, your content will overwrite. See that, suppose 255 00:13:38,100 --> 00:13:39,300 I am going to write here, 256 00:13:39,300 --> 00:13:41,100 [no audio] 257 00:13:41,100 --> 00:13:43,000 let me delete these two lines. 258 00:13:43,000 --> 00:13:45,800 [no audio] 259 00:13:45,800 --> 00:13:46,800 So what I am doing is, 260 00:13:46,800 --> 00:13:49,000 [no audio] 261 00:13:49,000 --> 00:13:50,800 "This is a new content". 262 00:13:50,800 --> 00:13:53,300 [no audio] 263 00:13:53,300 --> 00:13:56,700 Let me save it and run it. Done. Right. Now, 264 00:13:56,700 --> 00:13:57,700 see the content. 265 00:13:57,700 --> 00:14:00,600 [no audio] 266 00:14:00,600 --> 00:14:01,800 Your previous data is gone. 267 00:14:03,500 --> 00:14:06,100 Only new content is there. Previously you have three lines, 268 00:14:06,100 --> 00:14:08,500 right? But now you have only one line. 269 00:14:08,700 --> 00:14:12,100 The reason is, whenever if you open your file in 'write' mode 270 00:14:12,100 --> 00:14:15,100 simply you can assume that, if already file is there with 271 00:14:15,100 --> 00:14:18,100 this name that file we'll delete and new, with the same name 272 00:14:18,100 --> 00:14:20,900 new file we'll create. If file is not there, first time 273 00:14:20,900 --> 00:14:22,500 you're going to create that file. That's it. 274 00:14:22,500 --> 00:14:24,300 [no audio] 275 00:14:24,300 --> 00:14:25,800 Right. Fine. 276 00:14:26,200 --> 00:14:32,000 So guys here you are writing your content to your file one by one. 277 00:14:32,100 --> 00:14:35,100 I mean, suppose if I want to write three lines how you are 278 00:14:35,100 --> 00:14:37,700 going to write? Three times you are using 'fo.write', 279 00:14:37,700 --> 00:14:39,100 'fo.write', 'fo.write'. 280 00:14:39,400 --> 00:14:42,500 No need of that. See what I am doing is, I am going to comment 281 00:14:42,500 --> 00:14:45,700 these three lines. Instead of that, instead of that 282 00:14:45,700 --> 00:14:48,600 [no audio] 283 00:14:48,600 --> 00:14:52,600 'fo.writelines', and here you have to provide your 284 00:14:52,600 --> 00:14:53,800 data as a list. 285 00:14:53,800 --> 00:14:56,700 [no audio] 286 00:14:56,700 --> 00:14:58,600 You can also store your data somewhere 287 00:14:59,800 --> 00:15:02,500 as a list, let's say 'my_content', 288 00:15:02,500 --> 00:15:05,100 [no audio] 289 00:15:05,100 --> 00:15:08,300 that we have to take as a list. In that list each and every 290 00:15:09,000 --> 00:15:10,200 value is one line. 291 00:15:10,300 --> 00:15:14,700 Let's say, "This is a data - 1", suppose. 292 00:15:15,100 --> 00:15:17,900 So at the end you have to add '\n', right. 293 00:15:18,200 --> 00:15:20,800 So if you don't add you can also take care while writing 294 00:15:20,800 --> 00:15:22,900 your content to your file with the help of loop. 295 00:15:22,900 --> 00:15:26,100 I will show you that. Meanwhile, first to get basic 296 00:15:26,100 --> 00:15:27,400 idea I am giving in this way. 297 00:15:28,400 --> 00:15:30,800 "This is a data - 2". 298 00:15:32,100 --> 00:15:35,600 Then, "This is a data - 3", suppose. 299 00:15:37,300 --> 00:15:40,200 Right. See if I don't add here '\n', what will happen? 300 00:15:40,600 --> 00:15:41,700 Let me save it. 301 00:15:42,600 --> 00:15:44,800 See guys, instead of writing line by line with the help 302 00:15:44,800 --> 00:15:49,000 of 'fo.write' function, right, 'fo.write' method, 303 00:15:49,100 --> 00:15:51,500 what we are doing is, 'fo.writelines'. Here 304 00:15:51,500 --> 00:15:54,300 you can provide your content as a list. Now 305 00:15:54,300 --> 00:15:57,200 our content is a list. Somewhere we have our list of content 306 00:15:57,200 --> 00:16:02,800 here. Let me save it and run it. Same file name 'random'. 307 00:16:02,800 --> 00:16:05,000 Previously you have this information, right. Now, 308 00:16:05,000 --> 00:16:08,100 what we did? We opened that file in 'w' mode. 309 00:16:08,100 --> 00:16:11,700 That means, if there is any file with the same name that content 310 00:16:11,700 --> 00:16:13,900 will delete or that file will delete and new content 311 00:16:13,900 --> 00:16:17,200 we will write to this file. Now 312 00:16:17,200 --> 00:16:19,700 let me go here and open your file. See that. 313 00:16:21,200 --> 00:16:22,200 "This is a data - 1". 314 00:16:22,200 --> 00:16:25,400 "This is a data - 2". "This is a data - 3". In case if you want to 315 00:16:25,400 --> 00:16:29,400 send "This is a data - 3" to next line, what you have to do? At 316 00:16:29,400 --> 00:16:31,200 the end of your second line 317 00:16:31,200 --> 00:16:32,400 you have to write '\n'. 318 00:16:34,000 --> 00:16:37,400 Right. Fine. But providing '\n' at 319 00:16:37,400 --> 00:16:40,800 the end of each and every line it's not a good practice. Automatically 320 00:16:40,800 --> 00:16:43,800 you need to add '\n' for each and every line, right? 321 00:16:44,000 --> 00:16:47,400 So for that what I am doing is, now let me comment all these things. 322 00:16:47,400 --> 00:17:00,400 [no audio] 323 00:17:00,400 --> 00:17:01,400 Give me a second. 324 00:17:03,200 --> 00:17:05,098 Now I am going to write the different file name. 325 00:17:05,200 --> 00:17:06,598 I don't want you same file name. 326 00:17:07,000 --> 00:17:08,400 Of course, you can use it, no problem. 327 00:17:09,000 --> 00:17:12,200 Let me take first of all 'my_content' as something like 328 00:17:12,200 --> 00:17:14,000 [no audio] 329 00:17:14,000 --> 00:17:16,200 "This is using loop". 330 00:17:16,200 --> 00:17:18,300 Let me take, "- iteration- 1". 331 00:17:18,300 --> 00:17:20,200 [no audio] 332 00:17:20,200 --> 00:17:21,200 Then, 333 00:17:22,800 --> 00:17:28,400 "This is using loop - iteration- 2". 334 00:17:30,000 --> 00:17:32,599 Let me copy this data one more time. 335 00:17:32,599 --> 00:17:34,400 [no audio] 336 00:17:34,400 --> 00:17:36,000 Right. See that. 337 00:17:37,099 --> 00:17:40,200 So what I am doing is, this is a text, right, 338 00:17:40,900 --> 00:17:43,200 so let me take this as 3. Now, 339 00:17:43,200 --> 00:17:44,099 first of all guys, 340 00:17:44,300 --> 00:17:49,300 first of all I can take one by one value from your list 341 00:17:49,300 --> 00:17:50,300 using for loop, right? 342 00:17:50,400 --> 00:17:53,800 If you remember 'for each_line', let me take, you can take any 343 00:17:53,800 --> 00:17:56,300 variable here 'for each_line in my_content'. 344 00:17:57,200 --> 00:18:00,100 Right. See what I am doing, 'print(each_line)'. 345 00:18:00,700 --> 00:18:03,900 Save it. Run it. You're getting one by one line. 346 00:18:04,800 --> 00:18:07,100 Now before printing your line, at the end 347 00:18:07,100 --> 00:18:11,700 I am adding '\n'. You have no problem. You can add it no. Yes. 348 00:18:13,000 --> 00:18:16,300 Now, what I am doing is I want to create a file with this 349 00:18:16,300 --> 00:18:18,700 content, whatever the content you have in your list. 350 00:18:19,400 --> 00:18:21,300 So I want to create a file called, 351 00:18:22,400 --> 00:18:26,200 let's say, 'with_loop.txt'. 352 00:18:27,100 --> 00:18:28,400 I want to create this file. 353 00:18:28,400 --> 00:18:30,400 So open in 'write' mode. 354 00:18:31,400 --> 00:18:33,500 So it is going to give a file object. 355 00:18:33,700 --> 00:18:34,900 Actually, that is a class, 356 00:18:34,900 --> 00:18:36,700 okay. As of now, you can't understand that. 357 00:18:37,000 --> 00:18:41,400 Just assume that 'fo' is a cursor. Just for your understanding 358 00:18:41,400 --> 00:18:44,800 purpose I am giving 'fo' as a cursor guys, right. If you assume 359 00:18:44,800 --> 00:18:47,400 that it is going to work the same operation whatever your 360 00:18:47,400 --> 00:18:49,000 cursor is doing in GUI mode. 361 00:18:49,500 --> 00:18:53,200 So just now we got into our hand cursor position of this file. 362 00:18:54,000 --> 00:18:55,000 Now what we have to do, 363 00:18:55,600 --> 00:19:01,800 we want to write line by line content to your 'with_loop.txt' file. 364 00:19:01,900 --> 00:19:04,300 See, you know, if you have one single line how you're going 365 00:19:04,300 --> 00:19:08,800 to write your data, 'fo.write'. That's it. 366 00:19:09,200 --> 00:19:13,000 See what I am doing is, automatically I am taking line by 367 00:19:13,000 --> 00:19:17,000 line from your list, and I am writing that line to your file 368 00:19:17,000 --> 00:19:20,500 with the help of 'fo.write' method, and then while writing 369 00:19:20,500 --> 00:19:24,400 at the end of each and every line I am adding '\n'. Now 370 00:19:24,400 --> 00:19:27,100 no need to manually write '\n' at the end of each 371 00:19:27,100 --> 00:19:27,900 and every line, right? 372 00:19:28,700 --> 00:19:32,000 Actually, this is not a practice, good practice because you are going 373 00:19:32,000 --> 00:19:34,900 to get your data from somewhere, there maybe number of 374 00:19:34,900 --> 00:19:36,900 lines, right? 375 00:19:37,200 --> 00:19:40,500 So logically you need to '\n' at the end of your each 376 00:19:40,500 --> 00:19:43,300 and every line while adding that data to your file, that's it. 377 00:19:44,800 --> 00:19:48,800 Right. So after this what you have to do? You completed your for 378 00:19:48,800 --> 00:19:50,700 loop suppose. For three iterations 379 00:19:50,700 --> 00:19:53,400 this loop is going to over. Then after loop 380 00:19:53,400 --> 00:19:55,200 don't forget to close your file. 381 00:19:55,800 --> 00:19:59,700 Once if you close your file, automatically the data will store, 382 00:20:00,000 --> 00:20:03,200 will save into your file called 'with_loop.txt' file. 383 00:20:03,900 --> 00:20:05,700 Let me save it and run it. 384 00:20:06,700 --> 00:20:11,200 Done. Now go to here. Somewhere see 'with_loop' 385 00:20:11,200 --> 00:20:13,700 is there. Open that. See that. 386 00:20:13,700 --> 00:20:15,800 [no audio] 387 00:20:15,800 --> 00:20:17,100 Right. Fine. 388 00:20:17,600 --> 00:20:21,700 See guys, suppose if you have some file somewhere, old file 389 00:20:21,700 --> 00:20:25,300 is there, and you don't want to disturb the existing data, 390 00:20:25,300 --> 00:20:28,400 and you want to append the new data to your file then nothing 391 00:20:28,400 --> 00:20:33,600 is there. Simply open your file in mode, 'a', 'append'. At the end 392 00:20:33,600 --> 00:20:35,200 you are going to add new data. 393 00:20:35,200 --> 00:20:37,200 [no audio] 394 00:20:37,200 --> 00:20:40,900 Right. Let me save it and run it once. See guys, previously you 395 00:20:40,900 --> 00:20:44,200 have the content in your file, three lines content. 396 00:20:45,000 --> 00:20:48,700 Now, what I am doing is I am opening my file in 'append' mode. Whenever 397 00:20:48,700 --> 00:20:52,400 if you open your file in 'append' mode if this file is existing, 398 00:20:52,600 --> 00:20:54,500 it won't disturb the existing data. 399 00:20:54,500 --> 00:20:58,700 It will keep the data as it is. Then the new data will be appended 400 00:20:58,800 --> 00:21:00,000 at the end of your file. 401 00:21:00,300 --> 00:21:01,300 So same content 402 00:21:01,300 --> 00:21:03,500 I want to add once again to my file. 403 00:21:04,400 --> 00:21:09,100 Yes, just open in 'append' mode, right. See that. I'm running. 404 00:21:10,000 --> 00:21:12,000 It's done. Now open your file. 405 00:21:12,000 --> 00:21:13,900 [no audio] 406 00:21:13,900 --> 00:21:17,500 See that. Your data is there two times. Once again, you can 407 00:21:17,500 --> 00:21:21,400 run, so that once again the same data you're going to append 408 00:21:21,400 --> 00:21:24,600 at the end of your file. Now three more extra lines. 409 00:21:24,600 --> 00:21:25,800 Let me open that file. 410 00:21:27,500 --> 00:21:32,600 Yes. Right. Fine. Now assume that we don't have this file. 411 00:21:33,100 --> 00:21:34,700 Let me remove this file. 412 00:21:34,700 --> 00:21:37,000 [no audio] 413 00:21:37,000 --> 00:21:41,600 Guys, whenever if you try to append a data to this file, if 414 00:21:41,600 --> 00:21:45,500 it is there then the new data will be appended at the end. If 415 00:21:45,500 --> 00:21:46,800 it is not there, no problem 416 00:21:46,800 --> 00:21:48,700 your Python will create a new file now. 417 00:21:49,400 --> 00:21:53,500 So if file is not there then 'append' mode and 'w' mode both are same. 418 00:21:55,000 --> 00:21:58,600 If file is not there 'append' mode and then 'write' mode both 419 00:21:58,600 --> 00:21:59,900 are same. Now 420 00:21:59,900 --> 00:22:01,600 let me save it and run it. 421 00:22:01,800 --> 00:22:05,400 I am running. It's done without any errors means, it worked 422 00:22:05,400 --> 00:22:07,600 perfectly. Let me open your file. 423 00:22:07,600 --> 00:22:11,800 See now you've got 'with_loop' file, three lines of content. Now, 424 00:22:11,800 --> 00:22:12,800 I am going to rerun. 425 00:22:12,800 --> 00:22:15,800 [no audio] 426 00:22:15,800 --> 00:22:17,600 So already 3 lines of information is 427 00:22:17,600 --> 00:22:20,300 there no, because of this for loop. Same content you are going 428 00:22:20,300 --> 00:22:23,600 to write once again, three lines of information, right? 429 00:22:23,600 --> 00:22:25,400 Let me open it and see the result. 430 00:22:27,000 --> 00:22:29,400 So that is the use of your 'append' mode. 431 00:22:30,300 --> 00:22:33,400 That is the use of your 'append' mode. That's it. 432 00:22:34,400 --> 00:22:38,100 So guys, this is the way how you can create a new file or 433 00:22:38,100 --> 00:22:41,600 how you can append new content to your existing file. 434 00:22:43,100 --> 00:22:44,300 Right. Fine. 435 00:22:44,400 --> 00:22:47,700 Now, what I want to do is, I have some file, right. Already 436 00:22:47,700 --> 00:22:50,100 we know we have file called 'with_loop.txt'. 437 00:22:50,100 --> 00:22:52,700 Now, I want to read the content using your Python. 438 00:22:53,400 --> 00:22:55,800 I want to read the content using your Python. 439 00:22:56,100 --> 00:22:59,700 Nothing is there. First whether you want to read or write your content 440 00:22:59,700 --> 00:23:02,700 with your Python, first of all you have to open your file, 441 00:23:02,700 --> 00:23:09,100 but while opening your file, right, you have to write your required 442 00:23:09,100 --> 00:23:12,600 mode here, whether you want to write or read. Now my purpose is, 443 00:23:12,600 --> 00:23:13,700 I want to see the data, 444 00:23:13,700 --> 00:23:16,800 I want to read the data, then open your file in 'read' mode. 445 00:23:17,800 --> 00:23:21,800 That's it. So whether you're going to write your content to your file 446 00:23:21,800 --> 00:23:25,500 or whether you are going to read content from your file you 447 00:23:25,500 --> 00:23:27,200 have to do one thing open and close. 448 00:23:27,200 --> 00:23:28,400 Don't forget about this. 449 00:23:28,800 --> 00:23:33,100 If you don't close, then sometimes your file may corrupt. 450 00:23:34,800 --> 00:23:35,800 Right. Fine. 451 00:23:36,000 --> 00:23:38,300 Now, I opened in 'read' mode. 452 00:23:38,300 --> 00:23:40,000 I closed it, right. 453 00:23:40,100 --> 00:23:41,100 Let me execute it. 454 00:23:42,200 --> 00:23:45,700 No error. No output. Whenever if you are reading means, you should 455 00:23:45,700 --> 00:23:49,000 display something then only you can see the content. Now, 456 00:23:49,000 --> 00:23:50,900 how can I display the content? Nothing 457 00:23:50,900 --> 00:23:54,100 is there. Simply 'print(fo.read)'. 458 00:23:55,300 --> 00:23:59,500 'fo.read'. See the content. You're going to get your entire 459 00:23:59,500 --> 00:24:02,100 data at a time using 'fo.read' operation. 460 00:24:02,900 --> 00:24:05,400 You can also store these into one variable called, 461 00:24:05,400 --> 00:24:09,400 let me take 'data', data of your file. 462 00:24:09,400 --> 00:24:11,500 [no audio] 463 00:24:11,500 --> 00:24:14,200 That's it. After closing your file 464 00:24:14,400 --> 00:24:17,300 you have your data in a 'data' variable. 465 00:24:18,000 --> 00:24:22,800 That's it. See I opened, I read the data, and I stored into some 466 00:24:22,800 --> 00:24:25,700 variable called 'data'. Now, even though if you close your file, 467 00:24:25,700 --> 00:24:29,000 already we stored the file content to some variable, that 468 00:24:29,000 --> 00:24:30,100 variable we can use it. 469 00:24:30,100 --> 00:24:32,800 [no audio] 470 00:24:32,800 --> 00:24:34,200 Right. Fine. 471 00:24:34,200 --> 00:24:37,100 So at a time if you want to read your entire content, this 472 00:24:37,100 --> 00:24:38,100 is the procedure. 473 00:24:38,300 --> 00:24:41,300 Now, what I want to do is I want to read one by one line. 474 00:24:42,700 --> 00:24:43,700 One by one line 475 00:24:43,700 --> 00:24:46,700 I want to read. See that. What I am doing. 476 00:24:46,900 --> 00:24:49,700 Anyway, if you want to read always you have to open your 477 00:24:49,700 --> 00:24:50,700 file in 'read' mode. 478 00:24:51,200 --> 00:24:54,800 Then 'fo.readline'. That's it. 479 00:24:55,600 --> 00:24:59,700 Then let me close it and see the content what you are going to get. 480 00:25:00,600 --> 00:25:04,000 Sorry. You read the data, but you need to print that, right, 481 00:25:04,000 --> 00:25:05,100 then only you can see the data. 482 00:25:06,500 --> 00:25:08,800 'print', whatever the data you are reading. 483 00:25:10,000 --> 00:25:13,900 That's it. This is a, "This is using loop- iteration- 1". Now 484 00:25:13,900 --> 00:25:16,800 I want to read second line, then immediately do once again 485 00:25:16,800 --> 00:25:17,900 your 'readline'. 486 00:25:18,900 --> 00:25:19,900 That's it. 487 00:25:21,200 --> 00:25:24,400 But reading line-by-line, I do not know how many lines you 488 00:25:24,400 --> 00:25:25,400 have in your file. 489 00:25:26,600 --> 00:25:29,900 Right. So this is not a good practice to read your file. 490 00:25:31,100 --> 00:25:34,300 But you have an option. Sometimes, very rare cases you will 491 00:25:34,300 --> 00:25:38,800 use this one also. But remember that usage as well. Now, I 492 00:25:38,800 --> 00:25:41,300 want to read your entire content at a time. 493 00:25:42,500 --> 00:25:45,100 So previously whatever the data you are reading with the 494 00:25:45,100 --> 00:25:49,800 help of 'fo.read', right, see that, let me enable this once. 495 00:25:48,100 --> 00:25:56,400 [no audio] 496 00:25:56,400 --> 00:25:57,800 Let me 'print(data)'. 497 00:25:58,100 --> 00:26:01,000 And what is the data you are having in that? You know, you 498 00:26:01,000 --> 00:26:02,600 have a data, your data, right? 499 00:26:02,800 --> 00:26:03,800 See the output. 500 00:26:03,800 --> 00:26:04,900 What is the type of your data? 501 00:26:04,900 --> 00:26:05,900 That is a string. 502 00:26:05,900 --> 00:26:07,900 [no audio] 503 00:26:07,900 --> 00:26:10,700 Right, your string. You have number of lines in your file. 504 00:26:11,200 --> 00:26:13,900 I want to take line-by-line separately, but I want to read 505 00:26:13,900 --> 00:26:15,400 at a time all the lines. 506 00:26:16,900 --> 00:26:19,900 I want to read all the lines at a time, but I want to take 507 00:26:20,000 --> 00:26:22,300 line-by-line separately, not like a string. 508 00:26:22,300 --> 00:26:24,300 [no audio] 509 00:26:24,300 --> 00:26:25,200 See what I am doing. 510 00:26:25,200 --> 00:26:29,200 What I am doing here is, you have one more option just to read 511 00:26:29,300 --> 00:26:30,800 your entire content. 512 00:26:30,800 --> 00:26:32,600 [no audio] 513 00:26:32,600 --> 00:26:36,600 into a list. First of all, let me open my file, 514 00:26:36,600 --> 00:26:40,300 [no audio] 515 00:26:40,300 --> 00:26:48,100 then let me take 'data=', your 'fo.readlines'. 516 00:26:48,100 --> 00:26:49,600 It's very, very important. 517 00:26:50,200 --> 00:26:52,000 It is very useful in your real time. 518 00:26:53,500 --> 00:26:56,400 Now, let me print your data, and see the output what you are 519 00:26:56,400 --> 00:26:58,000 going to get. You're getting as a list. 520 00:26:59,700 --> 00:27:02,400 You're getting as a list. If it is a list, you know, you can 521 00:27:02,400 --> 00:27:05,700 take for loop, and you can print line by line. Suppose 522 00:27:05,700 --> 00:27:08,700 I want to read, simply I'm giving. If I want to read first three lines, 523 00:27:09,000 --> 00:27:10,700 What I will do is, see that. 524 00:27:10,700 --> 00:27:13,700 [no audio] 525 00:27:13,700 --> 00:27:16,800 'for each in range(3)'. 526 00:27:18,400 --> 00:27:19,400 First let me print 527 00:27:19,400 --> 00:27:20,900 what is the 'each' value you are getting. 528 00:27:22,600 --> 00:27:26,500 There is no relation as of now between your for loop and 529 00:27:26,500 --> 00:27:27,600 your data. Simply 530 00:27:27,600 --> 00:27:31,100 I am taking some 'range(3)', it is going to print 0, 1, 2. 531 00:27:32,800 --> 00:27:36,700 Right. Now this data is a list, right. In list 532 00:27:36,700 --> 00:27:40,800 if you want to read first three values, then index values 533 00:27:40,800 --> 00:27:44,100 are from 0 to 2, right, 0, 1, 2. 534 00:27:44,400 --> 00:27:47,200 So if we take 'range(3)', you are getting your required 535 00:27:47,200 --> 00:27:50,600 index values for your list to get your top three lines. 536 00:27:51,100 --> 00:27:55,500 Now, what I am doing is I am printing your 'data', then index value. 537 00:27:55,500 --> 00:27:57,400 [no audio] 538 00:27:57,400 --> 00:28:00,300 Guys, for first iteration your 'each' is equal to 0. That means 539 00:28:00,300 --> 00:28:04,800 you are printing simply 'data[0]'. Then second iteration 540 00:28:05,100 --> 00:28:07,800 you are getting index value as 1, third iteration 541 00:28:07,800 --> 00:28:10,200 you are getting index value as 2. 542 00:28:11,600 --> 00:28:14,500 That's it. This is the data you're going to print using your for loop. 543 00:28:14,500 --> 00:28:16,800 That means, you're getting top three lines. 544 00:28:16,800 --> 00:28:18,900 [no audio] 545 00:28:18,900 --> 00:28:19,900 Run it. 546 00:28:19,900 --> 00:28:22,200 [no audio] 547 00:28:22,200 --> 00:28:25,700 Right. So guys, now what I want to do is I want to get only last 548 00:28:25,700 --> 00:28:29,700 line. I want to get only last line, how can I get it? You know, 549 00:28:29,700 --> 00:28:31,700 [no audio] 550 00:28:31,700 --> 00:28:32,700 'data[]', 551 00:28:33,300 --> 00:28:37,500 last line, always. Already 'data' is a list, right. 552 00:28:37,500 --> 00:28:40,800 In list, last value index in terms of negatives, 553 00:28:41,100 --> 00:28:43,700 right, -1. Then I can print this one. 554 00:28:45,200 --> 00:28:46,600 Let me comment your for loop. 555 00:28:46,600 --> 00:28:52,300 [no audio] 556 00:28:52,300 --> 00:28:53,300 That's it. 557 00:28:53,300 --> 00:28:57,100 [no audio] 558 00:28:57,100 --> 00:28:58,400 Right. So guys, 559 00:28:58,400 --> 00:29:00,600 this is the usage with 'readlines'. 560 00:29:00,900 --> 00:29:05,100 We have so many real time cases, right. This is just introduction. 561 00:29:05,100 --> 00:29:09,000 We will see some simple real time example in next video. 562 00:29:10,300 --> 00:29:14,200 Okay. So guys finally you have to remember that, right. 563 00:29:14,600 --> 00:29:15,800 See 'write' mode, yeah. 564 00:29:15,800 --> 00:29:18,800 While working with your files you have three options. 565 00:29:19,600 --> 00:29:22,700 First option is you're going to create a new file. First time 566 00:29:22,700 --> 00:29:25,100 you are going to create. Then open in 'w' mode. 567 00:29:26,100 --> 00:29:29,700 So while opening your file in this mode, if already file is 568 00:29:29,700 --> 00:29:32,600 there then what it will do? Your Python is going to delete 569 00:29:32,600 --> 00:29:36,400 that file and is going to create a new file. So that, what 570 00:29:36,400 --> 00:29:40,500 will happen? Old file information will be deleted. So be careful 571 00:29:40,500 --> 00:29:43,100 whenever if you are opening your file in 'w' mode. 572 00:29:44,800 --> 00:29:50,200 I want to keep old content as it is and I want to add new 573 00:29:50,200 --> 00:29:54,400 content, I want to append new content to your file, 574 00:29:55,200 --> 00:29:59,500 then you have to open that in 'append' mode. Now, 575 00:29:59,500 --> 00:30:02,200 I want to read all the content which is there in my file, 576 00:30:02,200 --> 00:30:03,300 then open in 'read' mode. 577 00:30:03,800 --> 00:30:08,600 So whether you are going to write, append, or read, right, procedure 578 00:30:08,600 --> 00:30:09,700 is same. See, 'write', 579 00:30:09,700 --> 00:30:13,100 'fo', file object, open your file, and get into your hand your 580 00:30:13,100 --> 00:30:17,800 cursor. Do your logic here. Finally close your file. That's it. 581 00:30:17,800 --> 00:30:19,800 [no audio] 582 00:30:19,800 --> 00:30:22,200 Right. Fine. 583 00:30:23,400 --> 00:30:25,400 Okay guys, thank you for watching this video. 584 00:30:25,400 --> 00:30:39,424 [no audio]