-: Up until this point,
we've learned how to read files,
but we had 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 gonna say is,
with 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 with statement,
this is the proper way
to work with files in Python.
And if I save here
and run my script,
oh, I get an error,
because I forgot the colon 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, two, R,
which stands for read.
So with mode R,
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 write.
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 r 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.
Okay, 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 wanna have text,
and in this text,
I am going to say my file dot write,
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 is this 11?
Well, this isn't really important right now.
What we wanna focus on is,
hey, it's me,
I've written to 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,
alright, it looks like I wrote to the file,
but, uh oh, is this what we wanted?
When we write something to a file,
the cursor resets.
Remember, when we open a file,
the cursor goes to zero
and we write to the file,
and, if there's something already existing in there,
well, we override 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 wanna 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 at 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 write,
it will assume this is a new file,
and we just wanna, well, just add the smiley face.
If I click run here,
you see that now is just smiley face.
So you wanna 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 dot text.
Or actually no, let's do sad.
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 sad dot 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 write here,
and I run this, 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, sad face text.
So write also, creates a new file,
if it doesn't exist already,
or overwrites the existing one.
So you wanna be careful, once again,
with the mode, if whether you wanna append,
whether you wanna read,
whether you wanna read and write.
But, most of the time
the most common ones are read, write, and append.
Now, in the next video,
I wanna 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 gonna explore that topic
in the next video.