YTread Logo
YTread Logo

Python Tutorial: File Objects - Reading and Writing to Files

Jun 03, 2021
Hello, how are you all doing? In this video, we will learn how to work with

file

objects

in Python and some of the useful things we can do with these

objects

. Whether you use Python for desktop or web applications, you probably interact with

file

s quite a bit, so it's definitely a good skill to know how to properly interact with these file objects. Okay, let's go ahead and drill down to get a file object. We can use the built-in open command. I have a file here called test.txt in the same directory as my Python file. Now if I open this file, we can see that it is just a plain text file with several lines, so let's see how we can open and read this file from Python. now, the way I'm going to open the file now is not the way that is normally recommended;
python tutorial file objects   reading and writing to files
It's generally recommended to use a context manager which I'll show you here in just a second, but to show you why a context manager. is useful, let me first show you this method of opening

files

, so what we're going to do here is we're going to say F equals open and we're just going to open that test dot txt file now if you're working with

files

from different directories, you're going to have I have to pass the path to that file to the open command, but since this file is inside the same directory as my Python file, then I can just pass the file name, but if you want to learn more about how paths work, let's touch on that a little in the

tutorial

I did in the operating system module.
python tutorial file objects   reading and writing to files

More Interesting Facts About,

python tutorial file objects reading and writing to files...

Well, the open command here allows us to specify whether we want to open this file for

reading

,

writing

, adding, or

reading

and On

writing

now, if we don't specify anything, the default is to open the file for reading, but I usually like to be explicit here, so let's go ahead and say we want to open this file to read it and we can do that by passing a second argument here and that's just going to be the string of a lowercase R and we'll touch on some of these later, but if I wanted to write to a file then it would just be a lowercase W that would happen by adding to a file. is a lowercase a and if I wanted to read and write to a file, then I could do an R plus, but for now we just want to read the contents of the file, let's pass a lowercase R, okay, now the file is actually open. and we can print the file name if I were to print the name of point F and also before we run this and print the file name there is one more thing we have to do here if we open a file like I just did here then we need Explicitly close the file when we are done using it and to do this I will do this by saying F dot close so now that we have closed that file let's go ahead and run this and you can see that it printed the name of the file that we opened and this has more information which we can also print if we want to print the mode with which the file is currently open.
python tutorial file objects   reading and writing to files
I can do a dot mode F and if I think you can see it prints a lowercase R because we open the file to read it, so now even though this works the way we just did it, let me show you how to open the file using a context. manager and why in most use cases you will want to work with files this way, so if we open the file like we did here then we need to remember to close it explicitly, if we don't close it then you can end up with leaks that cause you run the maximum file descriptors allowed on your system and your applications might throw an error, so it's always important to make sure you close the files you open, so you can use a context manager, so it's kind of similar, but we can do this using the width keyword so we can say width and then I'm just going to copy all of this with open test.txt in read mode and then here at the end I'm going to say like F and then I'm going to open an opening for our block here .
python tutorial file objects   reading and writing to files
This can be a little confusing for people at first because the variable name is actually here on the right using like F instead of on the left when we said F. equals open, but the benefit of these context managers is that they allow us to work with files inside this block and after exiting that block of code, it will automatically close the file so that we don't have to worry if No, we add these closures here now, this will also close the file if any exception is thrown or something like that, which is why using these context managers is considered a best practice and it automatically takes care of all that cleanup for us. so now I'm going to go ahead and remove my external opening and closing statements.
Now, one thing that some people don't realize is that you actually have access to this variable F for now. I'm just going to say pass into this. context manager now we have access to this file object variable after exiting the context manager, but the file will just be closed, so for example, if I print the closed method in F now and run it, you can say, you can see him. returns true, but even though we have access to this variable here, it's closed, so it's not like we can read it, like if I try to read the contents of the file and print it, then you can see it throws an error value. here and it says I must operate on a closed file, so for what we want, we will have to work with this file from this context manager and for the rest of the video I will use these context managers to work. files since it's good practice, but first I wanted to show you the other way in case you see it in examples or wonder why I wasn't doing it that way.
Okay, let's go back to our file, so we tried to read the contents of the We closed the file and got our error, but let's see how we can read the contents of the file from here inside our context manager, so let's create a variable called F underscore content and this will just contain the content of our file now if we do an F. dot read and if I print this, oh, and I actually need to print the content of the underscore F, so if I save it and print it, then you can see that it It printed the entire contents of our file Now if you have a small file then this is probably what you want, but what if we have an extremely large file that we want to read but we don't want to load the entire contents of that file into memory?
Well, there are a couple more methods here that we have available to read the contents of the file instead of F dot read, so just to look at a couple of those, I could say lines of F dot read and if I print this, you can see that we get a list of all the lines in the file and it looks a little strange because we have our new line characters in there, but if we check this list, it actually gets each line in the file as a different element of that list now, instead of dot F read lines, I could do dot F read line and if I save it and run it now, you can see that read line took the first line of our file now, every time we run dot F read line it gets the next line in our file, so if I were to copy all of this and then do it again and run it now you can see it got the first and second line from the file.
Now this printed a little strange here because the print statement ends with a new line by default, but if I go up here and we pass an empty string to the end of our print statements, then it will no longer add that extra new line and now it can seeing that they are in the file, that's fine, but we still haven't solved our problem. how we can read the entire contents of an extremely large file if we read the entire file and everything at once, then we could run out of memory and we don't want to go back and read line F period, you know, thousands of times, so what we're going to do The thing to do here is instead of using read line or read lines, we can simply iterate over the lines in a file saying "let me go to a new line here for the line in F" and then from here we can just print that line, so I'm going to copy it and save it, so now let me go ahead and comment out these lines and run this iteration over the lines and you can see it printed all the lines in our file now this.
It's efficient because it doesn't read the entire contents of our file at once, so it's not a memory issue so we have to worry about what it will do: it will just go through and get one line at a time from the file. file now, this is usually enough for most people, but sometimes you might want more control over exactly what you're reading from the file now, if we go back, I'll go ahead and delete this line if we go back to our Line read point F here and I'm going to get rid of that one now I'm going to go back to read point F and if you remember this red and all the contents of the file, if I run it you'll be able to see it. which we got exactly the same thing with F dot read, we can actually specify the amount of data we want to read at a time by passing the size as an argument, so if I pass a 100 to our read method and then print This you can see that it printed the first 100 characters of our file instead of printing everything at once.
Now if I copy this and run it again, I can see that it printed the rest of the file. because it continued where it left off and read 100 more characters from the file now when we get to the end of the file the read will simply return a string MG so if I copy this a third time and run it again then you can see that nothing happens because what happens when we get to the end of the read file it just returns an empty string, so this print statement just prints an empty string. Ok, how are we going to use this technique to read a large file?
So since we don't know exactly how long the file will be, we'll have to use some kind of loop that only iterates over small chunks at a time, so instead of hardcoding it to 100, here I'm going to create a variable here called size to read and by now we'll just go ahead and set it to 100, so now if instead of passing 100 to F dot raid, let's pass this size to read, okay, this will take the first 100. The characters in our file now remember that when we get to the end of the file, the read will simply return an empty string, so if we do a while loop and say while the content length F is greater than 0, we will print the content that we got from the read now, don't run it like this yet because it will be an infinite loop, we never advance the content of the file after it prints the content and we want to read the next chunk of characters, so in order to do that then we just have to say again that the content F is equal to the reading of point F of that size fragment.
Now what it's going to do after this line here is it's going to return us to the while loop and check to see if we've reached the end of the file because F dot read is going to return an empty string and it's not going to satisfy this conditional, so now if I go ahead and I run this, you can see that it printed the entire contents of our file to get a better idea of ​​what's going on here, let's resize it to read at ten characters instead of a hundred characters and each time we print the contents of point F here instead of an empty string, let's make this an asterisk, so if I print this, then You can see that it's a little bit clearer that we're looping through ten characters at a time and it's printing these asterisks in each loop so you can see what happened through the loop here and printed these and then the asterisks which We know is just that chunk, then printed the next ten characters and then the next ten characters and so on until we get to the end of the file.
Now when we read the files, we can see that it advances its position each time, so I can actually see the current position using F dot tell, so what I'm going to do is I'm going to comment out this while loop here and down here, I'm going to say print and I'll print F dot tell, so if you Go ahead and run, you can see that F dot tells that it returned ten, which indicates that we are currently at the tenth position in the file and that is because we have already read ten characters here and we can manipulate our current position.
Using the find method, to show an example of this, let me print the first 20 characters of the file by running F dot read twice, so I'm going to go ahead and print the contents after the first 10 characters there and then. I'm going to do this a second time to get the next 10 characters and I'm going to go ahead and take out this second empty string so it removes our finished statement, so now let me get rid of F. dot L here and go ahead and run this so we can see that printed the first 20 characters of our file now, when we read this second fragment here, it picks up the tenth position and reads the next ten characters as we would.
That's to be expected, but what if I wanted that second read to start from the beginning of the file and we can do this with F point C, so between these two readings if I did a zero point F lookup, save it and run it? now you can see thatreset our position to the beginning of the file, so the second time we read our content it starts from the beginning instead of continuing where we left off after the first read. Now we use find zero to start. at the beginning of the file, you can use it to change the position to any location you want.
Okay, now let's take a look at writing to files. Much of this will be similar to reading. First of all, what if? we're trying to write from a file that we've opened in read mode, so let's go ahead and try that, so I'm going to do an F nothing right and I'm just going to do an F dot to the right of the test. I'm going to move on. and get rid of that while loop too and save it so you see when I have a file open in read mode and I try to write, we get an error saying it can't be written, so we have to have file.open in write mode, so now go back here inside our open statement, let's create a new file called test2 dot txt and instead of reading we're going to write to it now to do that, we can say a lowercase w instead of that lowercase.
R now you can see here in our directory that we don't have a test yet - txt. If the file doesn't already exist, this will go ahead and create it now. If the file exists, it will overwrite it. be careful if you are writing to a file that already exists now, if you don't want to overwrite a file you can use a lowercase a to add it to the file, but we will go ahead and Rove will write this file if it exists, so first of all instead of write to this file, I'm going to go ahead and put a previous statement in here that basically says don't do anything, so I'm going to go ahead and run this and you can see. which created test - txt, so I didn't actually have to write anything to the file to create it, just using open with write mode will create the file, so now to write to this file, we can do that. what we did before we can do a dot F write test.txt so I'm going to go ahead and run it now if we go here to test - txt then you can see that it wrote test in our file now if I go Come back here and write again in this file, then it will continue where we left off just like the read method did now.
If I run this and go back to the file, you can see that it wrote the test twice in a row. In fact, you can use search when writing files also to reset the position to the beginning of the file and we can do this if I go back here between these two write statements and now I was supposed to do a F dot C of zero if I run This then you can see that the second test overwrote the first, so C can get a little confusing with the right file because it doesn't overwrite everything, just what you need to overwrite, for example if instead of writing the same thing twice, if was to do a dot lookup F at the beginning and write and R as my second there and now, if I run that and go back to the file, then you can see that only the T was overridden and I test that it was not deleted. the rest of the content, so using file search every time I write to files can be a little confusing and I don't use it much, but maybe there are some use cases that you guys will find useful for, okay?
Let's go ahead and put this all together and use read and write on multiple files at the same time, so we'll use this to make a copy of our test dot txt file, so let's go ahead and delete our test2 dot txt file. here so we don't confuse the two and I'm going to go ahead and close that, okay, so I'm going to go ahead and get rid of these here, so let's first open our original test text file in read mode. and instead of F here I'm going to use our F and I'm just going to say our F, so read the file since this is the file that we're going to read to write to our copy, so now inside this with the statement here I go Let's go ahead and let's go ahead and copy all of this and paste another one open inside here.
I'm going to call this a test underscore text copy. I'm going to open this in write mode and I'm going to To call this WF to write a file now, you can put both open statements on a single line separated by a comma, but I think readability here is quite important and mix those two into a line is sometimes hard to understand, at least for me, so this is usually how I work with multiple files at once, placing them on two different lines, one nested inside the other. Now here we have two files open, our F to read our original file and WF to write to our copy, not in doing this is as easy as saying for the line in our F we want to make a dot WF to the right of that line, okay , now let's go over this one more time so we have our original file open and we're reading from that file and we have a file that doesn't exist yet, that's our copy and we're writing to that file and we're saying for every line in our original file write that line in WF, which is the file that we're copying to, so if you Go ahead and run it now you can see that it created this test copy of the dot txt file and if I open this you can see that it's an exact copy of our original ok and, lastly let's see how we can do something similar and copy a large image file now this is going to be slightly different so if I look in my current directory which has my

python

script that I'm currently running, I also have a photo of my dog ​​here when he was a puppy and let's go ahead and try. to copy this image file using file objects in Python now this file here is called Bronx jpg and if I try to replace our text files with these image files and down here I will call this underscore copy of the Bronx dot jpg now this is exactly Same as our previous example, but now we are trying to use an image instead of a text file.
If I try to run this, you can see that we have an error here that says that the UTF codec cannot decode the byte at position 0, so To work with images we are going to have to open these files and binary mode and the only thing that That means we are going to read and write bytes instead of working with text. Now I'm not going to go into the details, but if anyone is curious about the differences, I'll try to leave some resources in the description section on what exactly that means or, in this case, to work with these images to use binary mode, we can simply add a V to we read R here and we write W there, so now with that simple change, if I save it and now I run it, you can see that we have this image file copied here and if I go to the finder, you can see that The file was copied in exactly the same way as the original, so the last thing I said before was that sometimes you want to have more control over exactly what you're reading and writing, so instead of doing this line by line, let's do it in sizes of specific chunks and We saw something like this before when we were learning how to read our files, so to do this, let's just make a chunk size and we'll set it to 4096.
Now you can choose different sizes, but this is the one I'm choosing. here, now let's do an RF chunk and we're just going to read a chunk from our read file here, so I'm going to say RF point read and I'm going to pass in this chunk size, so now we're reading that amount of data. of our original image, so now let's create a loop that will write these fragments to our copy until there is nothing left to read from the original and if you remember what we did before, we can do a while loop and while the length of this fragment here is greater than zero, then we want to take our copy file and write that chunk to it, so I'm going to write this chunk to our copy and then to prevent this from being an infinite loop, I have to read the next chunk size, so I'm going to paste it there to read the next fragment of the original file, so now if I go up here and delete this copy that we just created, I'll delete it and now I'll move on. and I run it again using the code that we just wrote and you can see that it made that copy there and if I go back to the finder and open that copy, then you can see that it made an exact copy of our original, okay, so I think.
I'll do that for this video, there's a lot more we could see with files and I'll plan to prepare some

tutorial

s in the near future on how to work with temporary files in memory files and things like that, but I hope this was a good introduction to working with files and some of the useful things we can do with them now. If you have any questions, feel free to ask them in the comments section below and I will do my best to answer them. be sure to subscribe for future videos and thank you all for watching

If you have any copyright issue, please Contact