Up until this point, we've learned how to read files, but we have this annoying thing with the cursor
and then we had to close the file.
So how can we fix this?
Well, a better way to actually do something like this and do file IO with python is with the built
in with statement.
Let me show you instead of what we had before, what we're going to say is.
Width open.
The name of the file.
And say as whatever the variable you want.
So my file.
So now with open, you can actually do something like this, but you don't have to worry about closing.
So most of the time you want to use the width statement.
This is the proper way to work with files in Python.
And if I save here and run.
My script.
Nope.
I get an error because I forgot the column here.
If I run again.
There you go.
I get my output, and I don't need to close my file.
Everything is done for me.
So this is a standard way to read a file.
Now.
What if we wanted to write to a file?
So far we've only read and actually underneath the hood open has a default parameter called mode.
Equals to R, which stands for Read.
So with mode are we are able to read files.
When we don't specify the mode it automatically just assumes we're reading.
But if we wanted to write to a file, we can simply do W for right?
So if I run this.
You see that I get an error now.
Unsupported operation, not readable, because.
Well, the file can only be written.
So in order for me to read and write I can write.
Ah, w right.
Well let's see.
Nope.
That's actually not how it works.
You would think it'll be.
RW But this is a bit of a trick.
In order to read and write.
We do read plus.
So that if I run this.
All right.
I'm reading and writing and I get an empty list because this is now empty.
So let's write to a file here.
Let's make this a little bit smaller just so we can see that's better.
Let's say that in this file I want to have text.
And in this text I'm going to say my file dot.
Right.
And I can write whatever piece of text I want.
So let's say, hey, it's.
Me.
So now in here, if I do print text.
And I run this.
I get 11?
Hmm.
What does this.
11?
Well, this isn't really important right now.
What we want to focus on is, hey, it's me.
I've read into a file.
Okay, so let's say I wanted to open the file again and actually write again.
Let's say this time around, I'm going to write a smiley face.
If I run this.
And go back.
All right.
It looks like I wrote to the file, but.
Oh.
Is this what we want it when we write something to a file?
The cursor resets.
Remember when we open a file?
The cursor goes to zero.
And we right to the file.
And if there's something already existing in there, well, we overwrite it, so we have to be careful.
And by the way, this text file just gave us that we wrote two characters, the smiley face.
Okay.
So how can we fix this?
How can we make sure that we don't break what we have previously?
Which is, hey, it's me, and I just want to add a smiley face at the end.
Well, we can use the append mode.
The append mode is a and this appends to the end of the file.
If I run this look that hey it's me with a smiley face, it appends to the end.
So that's the append mode.
Again, we can use w to write.
And if I run this.
It adds a smiley face.
Now, why did it not do like last time and overwrite the existing thing?
Well, it's because of our mode.
W allows us to write to a file.
Previously when we did read and write.
What happened was we read the file.
Let's run this again.
Actually, let's go back to.
Hey, it's me the way it was.
And if I run this again.
Because we did read and write.
The cursor was set to zero and overrode it versus if we just did the right.
It will assume this is a new file and we just want to well, just add the smiley face.
If I click run here, you see that?
Now it's just smiley face.
So you want to be careful that you use the right mode based on what you need.
Now, let's say we wanted to create a file that doesn't exist.
Let's have a file called Happy Text.
Or.
Actually, no, let's do that.
And this writes a sad face.
But remember, we don't have this on our desktop.
If I clear this and look at our desktop, we don't have a set text.
If I do read plus here.
So read and write and I run this.
I get a file not found error.
If I do a right here and I run this.
All right.
It looks like it worked.
If I look at the files, I have a sad text now.
So now if I open it.
I have it sat face text.
So write also creates a new file if it doesn't exist already or overwrites the existing one.
So you want to be careful once again with the mode, whether you want to append, whether you want to
read, whether you want to read and write.
But most of the time the most common ones are read, write and append.
Now, in the next video, I want to talk about this idea of a file.
I've written text files for now, but we could have written anything.
We could have done a dot py file if I wanted to.
But what about accessing files that are in different locations, such as inside of folders?
We're going to explore that topic in the next video.