Okay, now we know how to open and read content out of the text file and in this
lecture we will go ahead and learn how to write content inside a file, a text file.
So we're still working with text files. Later we'll be using the same methods,
but applying those to binary files. So I'll go ahead and shift right click here
and open command window here, so that I have a command line open in this current
directory, therefore that allows me to pass relative paths when I work with
Python which is like directly example of txt. So if you open your command line
from here for instance as you know from C, you'd want to pass
full paths which would be something like D and colon and then backslash Dropbox
backslash ppfiles, backslash example dot txt. If you run Python from here.
For convenience it's good to open a directory in the current directory
in your working directory and mine is here. Pp backslash files. Now yeah, here is
the command line. I'll now go ahead and trigger a Python interactive session
there and I'll go ahead and create a file with Python again luckily the
syntax is the same almost, so you use an open method and store the file that
you'll be creating in a variable. Let's call this file, so let's name it example.txt.
And this time you don't want to pass r, you want to pass w which
stands for write. You execute that. And immediately you'll get this example dot txt
file and which for now it's an empty file.
So the open method what it does is it doesn't only open an existing file.
It also creates a file when that file doesn't exist, so in this case if
this example the text file was already in our working directory, then what Python
would do, it would open that file and that file would be ready to write content inside it.
When the file is not there, Python will create it, so now we can go ahead
and say file.write. We don't use read, we use write this time.
And there you pass the string that you want to write in there, let's say line 1.
It executes and you get some return there which is not very important, but for your
curiosity six means you wrote six characters to your file and now
probably wondering and looking at this example the text file which is still
empty, so it doesn't have the line 1
string written inside abd the reason to that is this example dot txt file is still
open inside Python and every change that you're making inside Python it only
exists inside the Python program, so you haven't yet saved that changes to your
example.txt file, and that means when you open it with another program such as
Atom you still get the old file. And if you'll try to delete this file, you'll
not be able to do so because that is opened in Python, so to write the
changes in there, you want to use the close method. Close and if you go to Atom,
you'll see a line one in the example.txt file and that's how
to write content inside a file. Something important that you should know is if you
want to add a second line to this example.txt file, you probably
would do, you'd open the file, so file open, example.txt in your write
mode. And then you'd probably add line two in
there and then probably close the file, but what you get is line 2 to only.
The reason to that is the W method is not an append method, so for adding more lines
to your existing text file, you'd want to use the append method which you'll learn
in the next lecture. So write method what it does is it
actually creates an empty file and then it allows you to write everything that you
want inside that empty file. That said, it's impossible to add
multiple lines using the write method, but you will have to do that in one
session, so that means you open, you create a text file and then say
file write line 1 and then file write line 2 and then
close the file and what you get is this text so you get both line one
and two. But that's probably not what you
expected exactly. What you are expecting it's probably a line 1 in a separate
line and line 2 under that, so to do that they remember that we had this
backslash n special character. That will allow you to create a new line just
after line one and then when you write line 2 and then close the file, you'll
get a text file with two separate lines. However if you had to write a long list
of lines there, let's say you have 20 lines, ou want to write and execute
in the write method 20 times wouldn't be very efficient, so what you do is use a
for loop to write all the lines using one single write method inside the loop
so normally you'd have at least of any other object. You may have an excel file
or CSV file with lines and let's say you have three items in there.
Line 1, line 2, line 3 and if you want to write these three lines in your example dot txt
file you'd want to iterate. Let's say for item in L which is a list file.write.
Item here would be in the first iteration, item would be line 1 and then
line 2, line 3, so you want to write item and maybe it's a good idea to use
backslash n, so after a line one you want to create a new line and then write a
line 2 after that, so item plus backslash n.
And yeah, that should do it. Nope that didn't do it because
Python cannot operate on a closed file. So this file variable is actually
a closed file object now and you want to open that first because you know we
closed it here and therefore you need to apply the open method again, so again
file and open, you open that file. And then go ahead and say for item in L
and we do this, execute. That was successful, but you still don't see the changes in
your txt file because you need to close the file.
And yeah. Line 1, line 2, line 3. That worked perfectly, so if you get this error
another time you know that you first need to open the file in Python, then you want,
then you apply write or read methods to that file.
So that's about the write mode. In the next lecture we'll go ahead
and append some more lines to this existing text file, so I'll see you there.