-
INSTRUCTOR: Hey, welcome
to Giraffe Academy.
-
My name is Mike.
-
In this tutorial, I
want to talk to you
-
about writing and appending
to files in Python.
-
So one of the cool
things about Python
-
is it allows you to work
with external files.
-
So I could have an
external text file,
-
and I could actually completely
read all of the information
-
in it.
-
I could parse through it
and use that information
-
to do certain things.
-
But in addition to reading a
file, I could also write a file.
-
And that's what I want to
talk to you guys about today
-
is writing new files and
appending onto existing files.
-
Over here, I basically just
have some code written out.
-
And this essentially
just reads information
-
from this employees.txt file.
-
So you can see over here, I'm
specifying the mode, which is r.
-
And that stands for read.
-
And then down here,
I'm just reading
-
all of the contents of the
file and spitting it out
-
on the screen.
-
So I'm going to click the
Play button over here.
-
And you'll see
that this executes.
-
So it's printing out all of the
lines of code in our text file.
-
So over here, I'm in
this employees.txt file.
-
And it just has all
this information,
-
like employees in an office.
-
But let's say that I wanted to
add another employee onto here.
-
Let's say that a new
employee joined our company,
-
so we wanted to add
them onto this list.
-
Well, I can come over here
to my app dot Python file.
-
And instead of
reading from the file,
-
I want to append it to the file.
-
So I want to say a.
-
And appending to
the file basically
-
means that you're adding some
text at the end of the file.
-
So wherever the
file ends, you're
-
just going to add
some text onto there.
-
So what we can do is we can
actually add another employee
-
into the file.
-
So instead of printing
something out,
-
I'm actually just going to
say employee_file.write.
-
And when I say
employee_file.write,
-
I'm going to be able to
write something to the end
-
of the file.
-
So I can basically just
write whatever I want.
-
So why don't we add
in another employee
-
into our employees.txt file.
-
So we can add in another
employee, why don't we say Toby.
-
And he's going to be
in human resources.
-
So now when I run
this program, it's
-
going to add Toby
- Human Resources
-
onto the end of the file.
-
So I'm going to run my program.
-
And you'll see that
nothing shows up
-
down here in the console.
-
But if I go over to
my employees.txt file,
-
all of a sudden, we have
a new entry over here.
-
It's Toby from human resources.
-
So I was able to append a line
onto the end of this file.
-
But here's the thing.
-
You need to be
careful when you're
-
writing to files
because you can actually
-
mess up a file very easily.
-
For example, I already added
Toby here into my file.
-
But if I was to run
this program again,
-
you'll see that over here
in this employees.txt file,
-
it went ahead and
added Toby again.
-
So it added this
employee here again.
-
And also, you'll notice
that in this case,
-
this employee didn't
go on to the next line.
-
I accidentally ran my file
again, and all of a sudden,
-
it messed up this
file over here.
-
And so appending, you
really need to be careful.
-
Because if you accidentally
run your file again
-
or if you append something on,
something wrong to the file,
-
it's permanent.
-
It's getting saved
inside of the file.
-
So I want to talk to you guys a
little bit more about appending.
-
Another thing we could do.
-
Let's say we wanted to
add another employee.
-
And you'll notice over here
in this employees.txt file,
-
when I appended it
on again, it got
-
appended to the end
of the existing line.
-
So the first time, I
had a new line there.
-
But if you don't have a new
line at the end of your file
-
and you want to add
a new line, you're
-
going to have to add
some special characters.
-
So let's add another employee.
-
And we'll call her Kelly.
-
And let's just say Kelly
is in customer service.
-
So Kelly is going to
be in customer service.
-
And if I want to
add this employee
-
onto the end of the
file, in a new line,
-
I'm going to have to put a new
line character in front of it.
-
So I can say
backslash n, and this
-
will append this entry into
the file with a new line, so
-
on a new line.
-
So now when I run
this, you'll see
-
we get Kelly from customer
service on her own line.
-
So you want to make
sure that you're
-
aware of these special
characters that you can use.
-
They call them
escape characters.
-
And anytime you're
adding on to a file,
-
you want to make
sure that you're
-
adding on exactly where
you want to add on.
-
So in addition to
appending to a file,
-
I could also just
overwrite a file,
-
or I could write an
entirely new file.
-
So since we already
have this open,
-
instead of appending
to the file,
-
why don't we just write a file.
-
So I'm going to use this w.
-
And now, if I say
employee_file.write,
-
because I'm using w and I'm not
using a, it's actually going
-
to override the entire file.
-
And it's only going to
put this inside the file.
-
So when I run this and we go
over to this employees.txt file,
-
you'll see we only have one
line inside of this file now.
-
It's just Kelly -
Customer Service.
-
That's because I
was using w, not a.
-
And when you use w, It's
just overwriting everything
-
that's in that existing file.
-
You can also use w
to create a new file.
-
So over here, I could
say employee_file
-
is equal to employees1.txt.
-
And now what's going to
happen is when I run this,
-
it's going to create
another file for me.
-
So I'm going to run this.
-
And you'll see over
here in my file browser,
-
we have this new
file employees1.txt.
-
So if I open this up, it
has exactly the same stuff
-
as in this employees file.
-
But it basically created
a new file for us.
-
And so a lot of
times you're going
-
to want to create a new file.
-
And you can use
different extensions too.
-
So if I wanted to create a web
page, I could say index.html.
-
And I could also add in
some HTML code in here.
-
So if you don't understand
HTML, don't worry about it.
-
But if you do, I could
put a paragraph in here,
-
another paragraph
like, this is HTML.
-
Basically, HTML is
like a web page.
-
And the point I'm
trying to make is
-
that you could write out a
web page inside of Python
-
by doing something like this.
-
So now when I play this, we
get this index.html file,
-
and it has some
HTML inside of it.
-
So that's one way that writing
to files can be really useful.
-
You can overwrite
an existing file.
-
You can write a new
file and create it,
-
or you can append on
to the end of a file.
-
And there's tons of applications
for writing to files.
-
And Python's a great language
for working with reading,
-
writing, and doing all
that stuff with files.
-
Hey, thanks for watching.
-
If you enjoyed the video,
please leave a like
-
and subscribe to Giraffe
Academy to be the first to know
-
when we release new content.
-
Also, we're always
looking to improve,
-
so if you have any constructive
criticism, or questions,
-
or anything, leave
a comment below.
-
Finally, if you're enjoying
Giraffe Academy and you want
-
to help us grow, head over to
giraffeacademy.com/contribute
-
and invest in our future.