Welcome back. Writing to a file is pretty similar to a reading from a file.
You still have to open a file object based on a name for the file.
But instead of reading from the file object, you'll write to it.
Let's see an example.
Here we're printing out the squares of all the numbers from zero up to,
but not including 13.
If I run it, we'll see we get zero times zero,
and one times one is one,
and then four, three times three is nine and so on.
Now, suppose instead of writing those to the output window,
we wanted to write them to a file where they would be permanently stored.
Well, let's start coding that up.
A normal little template for reading or writing from
a file is that we have some file object equals open,
add some file name,
I'll call it squares.text,
and now we have to say writing instead of reading.
Whenever I open a file object like that,
I tend to forget that I need to close it,
so I'll just put the close in right away.
Now, if I run this,
nothing different happens because I'm still printing to the output window.
So, instead of printing to the output window,
I want to write to a file.
So, I'm going to say, file_ obj.write instead of having a print statement,
and I'm going to have to turn that number zero,
one, four, or whatever into a string in order to be able to write it.
The print function is pretty forgiving.
We could give it a number or a string and it would figure it out but here,
we have to actually give it a string.
So, if I do this,
I will now have at the end in my file object all those numbers zero,
one, four, nine, and so on.
Now, we have a little simulator for
written files just as we have a simulator for reading from files.
In Runestone as you recall,
we can't read or write files from the local file system for security reasons.
So, we have built in disability to read a few files that are built into each page
and we can write files which will be available just until the page gets reloaded.
So, we have this file called squares.text and here's the output.
Now, that output may be a little different from what you were
expecting because we have zero,
one, four, nine, and so on,
it's not nice like it was in the output window.
We don't have zero,
one, four, each on its own line.
The reason for that difference is that when you call
print and you give it a string like four,
you'll automatically get four and a new line in the output window.
When we call.write, we just get the contents.
So, we just get the four,
but we don't get a new line character.
You have to decide for yourself when you want a new line.
So, what I'm going do is after I've written each square,
I'm also going to explicitly write a new line,
the backslash n character.
Now, if I save and run it,
we'll see something that looks a little nicer in the data file.
Now we've got all the values each one on its own line.
Of course, we could combine these onto a single line.
We could have the string of square plus \n all on one line,
that would work just as well.
Sometimes, especially for students who are just learning,
I like to make the new line character be
its own line because it's a real reminder that without write,
you have to create that new line character
explicitly unlike with the print function where it does it for you.
So, that works just the same.
Now, when we have a file for the duration of this page being displayed,
that file is available.
So, I could read it.
I can read that file, it's called squares.text.
So, new file object equals open of squares.text and this time,
I'm opening it for reading and let's just print out let's say,
the first 10 characters.
The.read gets me all the characters.
If I just want the first 10 characters,
I'll do that, and I will see something in the output window.
Let me clear all of my markings,
and now you see we've got the first 10 characters showing up in the output window.
The first character is a zero and then there's a second character for the new line.
So, that's two characters 3, 4, 5,
6, 7, 8, 9, and 10.
If I had asked for just the first nine characters,
I would have up to the one without the six,
and sure enough, there it is.
By the way, this file really is
there even if I don't recreate it each time with the code.
I could now just read it if I wanted to.
So, let me get the first 14 characters, let's say.
Let's say you'll see that we get something different,
and sure enough, we get the 25 in addition.
As I said, we've simulated the creation of this file,
it's there until we reload the page.
Let me just demonstrate that for you.
If I reload the page but try to keep this code,
we're going to have a problem,
it won't be able to find the file.
So, if I try to run the same code again,
it says that there is no such file or directory squares.text.
So, it can't open it because it hasn't been created.
That's how we write to files.
It structurally similar to reading.
We open the file,
just as we do for reading,
but when we're writing,
we do with a "w" instead of "r".
We call.write as needed but if we want a new line,
we have to explicitly write the backslash n character.
Then, we have to close the file.
It's especially important for writing because otherwise,
the contents might not be fully written and you might
lose some of them. See you next time.