2.5 File I/O - Opening, Writing and Closing

Video Activity
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *

Already have an account? Sign In »

2 hours 30 minutes
Video Transcription
Hello and welcome back to Intermediate Python here on Cyber Eri on demand. I is always in your instructor Joe Perry. And I'm very happy to have you here for lesson two, which is all about file I Oh, now you may remember in our last lesson, we talked about taking in, but from the user we talked about getting command line arguments
in bio. What we're really gonna focus on is instead of just direct interaction to the user,
had actually saved the data so that when your program is done executing it still exists on disk. The information is on disk for you.
So to do that, we're gonna jump in or B M again. And over here we're just going to very quickly explore the functions used to open and close a file, and then we're gonna play around. We're gonna write a program. It's actually going to write some user input into that file and save it for later reading.
So to play around here a little bit, we're gonna open or python three interpreter there screen pathologically,
and we're gonna say that f which is gonna be our file pointer equals open.
We're gonna give it. A name in this case is just gonna be test. Not that. And you could see the test. Not that is a string that's very important. If you give in to it without the quotes, the Python interpreter is going to think this is a variable and not know how to read it. So when you're calling this function, the file name is a strain as well as the second argument, which is the mode.
Now there are a few different modes, and we're gonna talk about most of them
others W which means open for writing. It's important to note that if you use W by itself, if the file already exists, that file will be overwritten and destroyed. So if you want to open a file that's already got data in it, you want to use it. The second mood, which is a which is a pen and that will open the file and start you at the end.
Additionally, you can use
if you just want to open the file to read it. You're going to use the our argument.
So those are the three core arguments that you can use when you're making this call Additionally, you can use W plus
a plus or are plus and essentially those mean open it in this mode, but also with the inverse. So if you open with our plus, you can read and write. If you open with a plus, you can write and read same sort of general thing. But so we're gonna just, in this case, open this file for writing. So
again, the argument here that we're giving his test Not that which is the string file name we want to create.
And second is the string mode, which is going to be W which is right. You can see here that that is stored in F
and we contest to see just by writing f here, and you can see that it is a text I owe wrapper, which is a python three interpretation that essentially just means this is a file.
We'll clear the screen again, and now we're gonna win it. We're going to explore the first method that we're going to be making use of here, which is f dot right,
And now I got the right function is a dirt simple function. All that it knows is you pass it a string, and it puts that string into the file at the current buffer location. We're gonna talk about the buffer location in just a second. So in this case, we're going to say this is a test strength,
and you could see that the return from that is the number of characters written in the file. If you count this, that will be accurate.
And we can just go ahead and close our file. Now.
Now we can just exit are Python interpreter. And we're going to see that despite the fact that the process we were writing in the process we're using is over
that, Prosser, That file now exists, and we can examine that file without python. We can save them test dot dat. You see, we have a test strength,
not real quick. We're actually gonna add a little bit more data to that so that we can do some examinations of a couple of different functions.
This sentence, it's false. That's just in case I have any robots watching this class, and I need to take him out.
that joke was terrible.
This is the last line.
All right, so we've added a few lines of text into test dot deck. We're gonna reopen our python interpreter here.
Wrong one.
And we're going to area clear a screen. So now we don't have that file. You can see that f doesn't mean anything. So we're going to do what? Wrong, Wrong command. Again, we're going to f equals
Just stop that.
And this time, we're gonna open it in breed mode. Now, if you try and open a file that doesn't exist in read mode, for example, tests one dot Dad, you're going to get an error because not able to find that file
Now, On the other hand, if you were to give it the argument
R plus
rather not our plus a There we go.
Even though that file doesn't exist for youto append to it when it opens that it's going to say, Oh, there When it looks for its to say, Oh, that file doesn't exist. No big deal created new one because this is a right mode
f dot glues.
Here we go. And again, we're going to do equals open.
It has stopped that
in a great mood
and we could see that f does in fact exist correctly. Now we're going to do We're going to look at a few different ways we can read from this. The simplest is just f dot read, which is just gonna return all of the tax that it can store from that file. And you can see here that it's going to return. It is a single string
of data, and you can see here has the new line in it. So instead of printing that string with formatting, it just gives you back that raw string.
If instead we wanted to do f dot re if you wanted to read it line by line and delineate delimit by those new lines we were going to do f dot read like
now this actually isn't going to work. And it's because of something that I mentioned a very briefly a little bit ago that we need to understand, which is the idea of a cursor or a buffer location. Now what's happened here is that once we pulled this F reed here, once we actually executed that the cursor, the buffer in the file actually went character by character
the same way we do.
If we were just a type. This is a test of strength
and the same way that by typing in our cursor, now exist to the end of the string. That's where the cursor is in that file. So when we called this read line, it doesn't give us anything back because we've already reached the end of the file in order to correctly use it. What we want to do is we want to use a function called F dot
and we're just going to give it the argument zero and zero just indicates, Started the beginning of the file
now U f that read line
and we could see that it read one line,
and we can keep doing this
until I get
to the point that it returns an empty string
and that empty strings are indication this is the end of the file. There's nothing left of you read
so very tribute. We That's how we're able to actually input and output from a file. That's gonna be the end of this video. In the second half of this video of the second half of the file, I Oh, we're going to write a program that's actually going to take user input and store it in a file for later retrieval.
So thank you for watching. I've been your instructor, Joe Perry, and I'll see you in our next video here in Intermediate Python on Cyber Eri on demand.
Up Next