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 *
or
Already have an account? Sign In »

Video Transcription
00:00
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
00:18
in bio. What we're really gonna focus on is instead of just direct interaction to the user,
00:23
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.
00:32
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.
00:47
So to play around here a little bit, we're gonna open or python three interpreter there screen pathologically,
00:52
and we're gonna say that f which is gonna be our file pointer equals open.
00:57
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.
01:15
Now there are a few different modes, and we're gonna talk about most of them
01:18
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.
01:37
Additionally, you can use
01:38
if you just want to open the file to read it. You're going to use the our argument.
01:44
So those are the three core arguments that you can use when you're making this call Additionally, you can use W plus
01:49
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
02:07
again, the argument here that we're giving his test Not that which is the string file name we want to create.
02:10
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
02:17
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.
02:27
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,
02:35
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,
02:53
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.
02:59
And we can just go ahead and close our file. Now.
03:01
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
03:08
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,
03:17
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.
03:25
This sentence, it's false. That's just in case I have any robots watching this class, and I need to take him out.
03:34
Uh,
03:35
that
03:38
that joke was terrible.
03:43
This is the last line.
03:47
All right, so we've added a few lines of text into test dot deck. We're gonna reopen our python interpreter here.
03:54
Wrong one.
03:55
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
04:06
open.
04:09
Just stop that.
04:11
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
04:21
Now, On the other hand, if you were to give it the argument
04:24
R plus
04:26
rather not our plus a There we go.
04:30
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
04:41
f dot glues.
04:46
Here we go. And again, we're going to do equals open.
04:50
It has stopped that
04:51
in a great mood
04:55
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
05:10
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.
05:17
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
05:26
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
05:46
the same way we do.
05:46
If we were just a type. This is a test of strength
05:50
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
06:05
see,
06:08
and we're just going to give it the argument zero and zero just indicates, Started the beginning of the file
06:13
now U f that read line
06:15
and we could see that it read one line,
06:18
and we can keep doing this
06:21
until I get
06:25
to the point that it returns an empty string
06:27
and that empty strings are indication this is the end of the file. There's nothing left of you read
06:32
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.
06:48
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
Similar Content