Welcome back.
Now, before we move on, I wanted to let you know that there are different methods
for writing and reading our files.
Now, so far, we've been dealing with the file dot write method, which is of course,
very straightforward.
So
if I run the program right now, it's gonna create my example dot TXD,
I open it up and there is Hello world now, file
write is useful and it's used
whenever we're, we're writing individual or very simple, our strings to our file.
Hello
world is very straightforward. It's very simple.
But what if we wanted to write like multiple strings or
in order to write large amounts of text file or write will no longer be used,
we'll have to use a different method.
So
as an example, right,
if I had a list of names, so let me create my list names.
Let me add the first name, Alex and
uh let me add the new line character. So
slash N
and let me allow
Python to add the remaining names. So
we have Alex Bob Caroline, Dave and Fred, right?
If I wanted to write these strings, I can no longer use file right.
I'll have to use a different method. So,
what I'll do right now is I'm gonna go with the usual R with
open and then brackets and then remember
we'll have to open up a file example dot CXT.
And then w of course two,
right?
We have a colon
and then I'm gonna say sorry as file and then a colon
and now
you can see file dot write lines
and then uh names.
So
write lines of the file dot Write lines.
This is used whenever we're writing multiple strings or
by writing large pieces of text onto our file.
So
if I want to put them right now
and I open it up, there you go. We have the names right there.
So please do be aware of this
in the future. If you're going to create
advanced programs that will require writing large amounts
of text or strings to a particular file,
you want to go with the file dot Write lines
as opposed to the usual file dot Right.
And just like with the writing, we also have different methods for reading.
Now, so far,
we've been using the very simple uh read the file dot read
and I just copy some code in here.
Let me put this in here real quick.
So, all right,
so far we've been dealing with code like this, right? We will have a file
and it will say content
calls file dot we and
there it is Right. So, if I run the program right now,
very, very simple. We have Alex Bob Caroline, Dave and Fred. Ok.
The thing about using file dot Read
is that
it's gonna read the entire file at once,
which is great whenever you're working with very small files. However,
if you're dealing with large files,
it will still work. Don't get me wrong, it will still work.
It's still gonna read everything.
The problem though is that because it was going to read the entire file at once,
it will have to use a lot of memory
in order to load all the text in that file
at once.
That's the problem.
It's not very efficient whenever you're dealing with large
amounts of text in a file like big files.
So you want to use a different method.
Now
let me show you,
I'm going to create a new program right here and say with open.
And now let's assume that example dot TXT in this
scenario has large amounts of text is a very,
very large file, right?
So
I'm gonna go to the usual vout, you know, add a file name and then R
and then say as file,
OK?
Now
check this out. I'm gonna say line
equals file
dot Readline.
Notice the difference. It's no longer a file dot Read, it's now file dot Read line
and then I can create
my up and say while line,
OK? So while our file is open, it has text in it.
We want
to print the line.
Ok?
And we can also then say line
equals file
dot
read
line.
And here it is.
If I was to run the program again,
you will see right now that we have just about the exact same output,
you're not really going to notice the difference
because we're dealing with a small file regardless.
However,
please do keep in mind in the future just like with the right method.
If you're gonna be dealing with large amount of files that you need to read from,
you want to use dot Readline,
if it's a very small file, file dot read
would be used. Ok? It's ideal for those for those kind of scenarios.
However, we are not done.
There's another method which is the file dot read lines.
What this does is that it's gonna read all lines at once and will return
them as a list of strings where each element is a line from the file.
In other words, it combines the benefits of both your read and your read line
by reading the whole file at once, just like the file dot read.
However,
it's gonna store each line as an individual element in a list.
So let me give you an example. I'm going to go back in here.
I'm going to modify this to change line to lines
equals file dot read lines.
OK?
And now instead of using the while loop,
I'm going to switch this over to the for loop and I'm gonna say four
line
in
lines,
colon.
And now we can simply print
in brackets
a line.
OK.
Or better yet,
let's add the strip method in order to remove any unnecessary spacing.
And there it is right. Now, if I was to run the program,
there you go,
Alex Bob Caroline, Dave and Fred,
again,
you're not going to notice the difference when
you're dealing with a very small file.
But once again, do keep in mind that if you're dealing with large files,
you want to go with either the red line or the read
lines.
The last method I want to show you is going to be the four line
in file.
And the purpose here is to read the line to read
the file line by line in a memory efficient way.
So I'm gonna go back in here.
All right, let me remove the lines.
So we have with open example dot TXTR file. So now I will say for line
in, I could say file,
OK? For line in file,
now simply print line dot strip. And then once
you run the program,
there you go.
This approach is actually very similar to using your read line,
but it's actually more concise
and considered the best practice for reading large
files because each iteration retrieves a single line.
So memory usage is going to be low.
So to summarize
if you want to read from a small file. The usual are read
our file dot We will be
the best.
If you're reading from a large file,
you can go with your line in file.
It's better than your file dot Readline and also better than your file dot read
lines.
Now, I also just want to point out that
in addition to the modes for your read mode, your append mode, your write mode,
we also have additional modes like your read and write mode,
your write and read mode as well as your append and read mode.
As an example.
Instead of R
here, I can say R and then plus
this is what we refer to as the read and write mode.
So this is going to open up a file for both reading and writing.
Keep in mind though that the file must exist
and content can be read and modified. All right. So your file must already exist
in order for you to be able to use this particular uh mode,
we also have the write and
read mode,
which is going to be W and then plus this
right here will open a file for reading and writing,
but it was gonna replace any existing content.
And then finally, of course, you have your append plus uh mode,
which is the append and read mode.
This will open up a file for reading, writing,
appending new content at the end
without changing any existing content. So
you're more than Welcome to play around with these
modes and see how they modify your file.
Thank you for watching. I'm gonna see you in the next class.