All right. So let's talk about file handling. And right now as it is
the only file we have been working with is the main dot py file.
It's just one Python file and nothing more.
However,
we could create other types of files.
What if we wanted to write a program that would
ask the user to provide their username or password?
And then we store
that information, that user information in a separate file.
How can we do that? But we can do that
in a variety of ways. And that's what you're going to learn here. So
the first thing I want to show you is how to create a file.
There is a statement called with
and then a function called open,
all right
brackets.
What this will do is that the open function typically accepts two parameters. All
you will have to provide the name of the file
that you want to work with.
And then the second parameter will be
whatever it is that you want to do to that file, maybe you want to read to the file,
write to the file, append the file and so on.
So as an example, right? Let me say
I want to open up a file called example,
OK dot TXT. So it's a text file, right?
And now comma
and now I have to provide
the operation. Do I want to open this file? Do I want to read the contents of the file?
What do I want to do?
I want to write to the file. So I'm going to use W
to represent the right function, the right operation.
And now to finish this, I'm gonna say
file.
So this right here,
this is typically the syntax, OK? You will say with
open and then in brackets provide the name of the file
and then the operator, whether it's W for write R for read and so on and then say S file.
Now I'm gonna jump over to the new line and say file dot write.
This is going to be the function that we're going to use
to actually
write the string or whatever it is that we want to write to the file. So
in brackets,
I'm gonna say
hello world,
this is what I want to write to the file.
Now,
check this out.
I'm gonna go ahead now notice so far, I still have only one file main dot py.
But if I was to run the program,
do you now see
it says example dot txt.
So even though I didn't, didn't have this file created before,
whenever you're writing to a file,
if that file hasn't existed before.
Then Python will automatically create that file for you.
Now,
if I was to open up my example dot text, you can see right now it says hello
World.
What if
I come over here right now and I write another
right function?
And I say
this is awesome.
What do you think is gonna happen? Let's go ahead and run the program.
If I go back to my example Doc TXT now you can see it says hello world. This is awesome.
So now it has added
the, this is awesome to our file. Let me go back and close this. Let me remove
the second file function.
All right,
now
we can see that hello world is in fact an example dot TXT.
But what if in our console, we wanted to display
the contents of example dot TXT.
What I will do here is I'll say with open again.
And now
in brackets once again the name of the file example dot TXT.
And now we're going to use the R function instead.
This is what we use for reading the file. So
I'm gonna say as file colon.
And now
what if we wanted to print out the contents of the file?
I can assign a variable called content
equals.
And now
I'm going to use the function file dot Read
this function right here. File dot We
will read the contents of our file.
It's going to pass the contents to the variable content.
And now all we need to do from here will be to simply say prints and then in brackets,
what are we printing, we're printing content.
OK?
Now I'm gonna run
and there it is right here in the console,
you can see we have hello world being displayed.
So
this
is basically an introduction to
reading from files, writing to files.
And one question you may have is OK,
can we actually create our files
without using the with statement?
And the answer in fact is yes, we can do so without using the with statement. So
how would we do this? Well,
let me just, first of all, remove the program in here. What we need to do.
First of all is to
say file
equals and then open,
this is going to be the actual open function
itself and remember that open accepts two parameters.
So now
we'll have to open up our example
dot TXT.
And now what I want to do, we want to write to the file. So I'm gonna say w
let me just remove, let me delete this example dot text file. OK? Let me delete it.
OK? So like we're starting from the very scratch, right?
So now
I have told Python, OK? I want you to create this file called example dot TXT.
What do we want to now write to the file?
I'm gonna come in right now and say file dot write,
OK?
Because now I'm writing to the file and then in brackets, let's provide the string.
Hello world.
And now what I would have to do
is I'm gonna have to close the file when you're opening
up a file or you're waiting to a file or you're
into a file
by default, the file needs to be opened in order for that operation to occur.
What you always want to do is you want to ensure that the file is cool.
Once the operation has finished, if you don't do this,
this can lead to programs
leaking out memory and it can be very vulnerable to different types of attacks. So
to close the file, very simply file dot what dot close
and that's it.
So now
if I run the program again,
you can say example, the text is over there,
I can open up the file and it's right there.
Hello world.
The reason why we prefer or most developers prefer to use the with statement
is because with the with statement, let me just undo all of this.
With the with statement,
you would notice that we didn't actually
I have to close the file manually.
Python is smart enough to know that. OK, when you're using the width statements
as soon as you've performed whatever operation it is
that you want to perform on the file,
close it automatically.
That's why developers often use the width statement.
You don't have to close the file anymore
because Python will do so automatically plus,
it's also cleaner.
You have less lines of code.
That's why the width statement is typically used in conjunction
with the open function.
So thank you for watching. I will see you in the next class.