>> Okay. So we've learned how to work with the file system,
now we're learning how to actually read and write actual files.
So let's suppose we have a file like this,
which we want to be able to read with our Python code.
So we have just three simple lines of text in
a file called demo.txt and we want to read that.
So what we're going to do is we're going
to open up a file stream saying,
let's read demo.txt, and we're opening it for reading.
We could have left this part RT off,
it will default to a text file reading.
In case you're having problems,
sometimes when you try to read files,
you never quite sure why isn't working,
maybe the file isn't there, maybe it's not readable.
These really common programming issues to run into.
So you might do things like check and see
if it's readable before you try to read it,
when you're trying to debug why you're getting an error.
Then you can read one or more characters
from the stream and then read to the end of
the line and then you can read
all lines to the file using read lines.
So if I run this code,
remembering this is the file we had,
this is the end of the first line,
second line and third line.
So if I run that code,
what you're going to see is,
and I'm just going to make this a little bit easier
for you to see the whole thing at once.
So is it readable?
Yes. True. So that's a good sign.
That's probably why the next few lines of code actually worked.
It reads one character T,
and then read to the end of the line, it continues,
it doesn't reread the T,
it continues from where we left off,
it is the first line of the file.
Then when you see do the read lines,
it continues again from where it left
off and reads the second line of the file.
You'll see the new line show up as
a newline character and then the last line of the file,
so you can see all the values that get returned.
Now, what if I want to write to a file?
This also comes up a lot in different scenarios.
Well then we have to create a file and open it for writing.
So this will actually create the file
for me because the file doesn't exist yet.
Again, we might want to just make sure that the file was
created and its writable before we start writing to it.
So I'm just going to do a quick test and check if
the file stream is writable before I start writing.
Then I can write a single string,
I can write multiple strings using write lines.
So write, you pass a string that you want to write,
write lines allows you to pass in
multiple strings and if you want a new line,
you actually need a newline character.
I'm just going to comment this part out for now,
and we'll come to that in just a second.
So if I run this code,
save those changes, go down here and run the code.
What's going to happen? It says, yes,
I can write to this file and over here on the left, look at that,
it's created the output.txt file and
inside it you can see hello world because I wrote
the H and then I wrote E-L-L-O and then I
said now write a space and now enter
a world and then it inserted in nice new line for me.
Now, what's really cool about using write lines is you can pass it
multiple strings and if you
have something like a list object maybe,
anything that you can iterate over that contains strings,
you can actually use write lines to say take
all the strings in that object that I
can iterate over and write that to my file.
So I can say take the names Susan and Christopher,
I could have that in a Python list for example and I can
write but all the names in the list to the file in one step.
I don't have to write a loop to do this
it's such a slick little feature.
So now if I write this again, run this code again,
and now we open output.txt,
you'll see Susan and Christopher written out,
so both the names are written.
Now, this is where I'm going to
give you a little extra, a little cool thing,
because a lot of times when you're iterating
through all the values in the list,
you might want each value to appear on a separate line.
So I'm going to show you
a little trick that will help you do that.
One of the things you can do is,
and I'm just going to start on a new line before I write Susan,
Christopher is you can use the join command.
So when you're doing the right lines,
you join a new line for each name.
Now you could easily do commas if you need to
make comma-separated variable lists and so on,
but in this case, I want each name to be on a new line.
So the join command allows me to,
after each value, include a new line with it.
So now what you're going to see is in my output file,
Susan and Christopher show up on separate lines.
So that's a neat little trick that might make your lives
easier when you're iterating through a list and using write lines.
I just want to finish off by showing you
another neat trick when using seek.
When you are writing to a file stream
and you write to a file stream,
remember you're not actually writing to the file,
you're writing to a file stream.
So you can actually move up and down
around the file stream as you're writing to it.
So if I decide to write the word demo with an exclamation mark,
and then I use the seek command,
seek allows me to reposition where I am in the file stream.
So making seek zero literally
moves me to the beginning of the file stream.
So then if I write again,
it'll actually overwrite
the first few characters with this new word.
So if I run that code so you can
see how it shows up in the output.
Now we see this manage.txt.
You can actually see that the word demo exclamation mark,
the first four characters were overwritten by that word cool,
but it's still kept that exclamation mark from the word demo.
So Seek allows you to go back and overwrite parts of the stream.
I do just want to take a minute here
to talk about flush and close.
I explained this in the slides.
So if you've already heard it there don't need to listen for.
Those of you who jumped to the code
because I know some of you like to do that,
flush what it will do is it will take
the contents of the file stream and write it to the actual file.
So that way if there's anybody else out
there who's opening that file,
they will actually see your changes as well.
Once you run that flush command,
anyone else opening the file will see those same changes.
But it doesn't actually mean this necessarily saved to disk,
the operating system will decide when to actually
save those changes to disk itself.
So just a small thing to be aware of.
You don't have to call flush explicitly when you call
the close command to close
your file and that's something very important,
you always want to close your files when you're done with them,
file streams when you're done with them,
that will automatically do a flush for you.
So not a lot of reason to call flush explicitly,
but it's a very common point of confusion.
So I just wanted to explain what it does.
Basically doing a flush by itself means
take the contents of file stream buffer right into the file,
doesn't mean it's saved to disk yet, just means in the file.
So if anybody else is opening up that file,
they'll see those changes and it just
empties out the file stream if it's getting big.
All right. So some cool new things.
Now you're ready to go.
Start working with files.