Read file

5 minutes
Share the link to this page
Copied
  Completed

Transcript

Hello everyone, welcome back. In this video, you will learn how to read a file from the program. First, let's create a to do list dot php file, you can download the one we have upload, or you can create a new one. File, New File. Then we can type in the task, one task per line. And then we save it.

Yes. Then we create a source code file, file, New File, Save As to do list.py we put it in the same directory as the to do this dot txt file and then We can press one. Yes, let's start reading the file to read the file, use open and the file name. To do this dot txt, then we specify the mo of using the file for read is our URL. Now we can read the file by file dot read line. If it were with the first line, if we do it again in with the second nine, if we do it again, it will be the third and four until we can nothing.

You may aware that there is a strange slash and at the end of each item, this means is a newline. This character is admissible in normal test editor, but in Python is being shown here. If you want to go to the beginning of the file, we Use file toxic with parameter zero, then we can wait from the beginning. We can also use for loop to handle the file content. First, let's go back to the beginning of the file. And then we use folder for line in file.

We light Yes, you see it we print the content of the to do lists. But you may notice there's something strange here, there's an empty line between two tasks task s because the print function will print a newline character after the string. And as we have discussed, the Li string actually contain a newline character at the end. So in total, there's two new line character and to do line is being printed as this is not what we want less to The behavior of pain. First, let's go back to the beginning of the file. And then we do for lie in file.

Now we've cleaned the lie, but we pass in an additional parameter called end. By default, it will print a newline character, but now I want it to print an empty string. Now it looks what we expected. Now we want to add the index number between the task like 1234. To do that, we can use the function enumerate. Before we do that, we go to the beginning of the file again.

And then we do the for loop. Now we get the index underlined and then move a pile Then we want to specify the starting number as printed, we want to print the index number first, we have the placeholder for that. And then we print the content, the task on that index and right. Again, we want to print an empty string as the ending character. Yes, here's what we want to see. After we finish all the operation, we need to close the file by typing file close.

But, you know, is quite troublesome to do that every time and most of the time programmer forget to do that. So there's another way to make things simple, is using the web. So we can do it by with open to the stock txt As file and then we do the folder, the printing that for index ly in a minimal way, file sapping for one. And then we create it. Yes, we have exactly the same output. And now we don't need to close the file manually, it will be close automatically.

This is how you win a file from a Python program. In the next video, we will talk about how to write a file. See you

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.