-
INSTRUCTOR: Hey, welcome
to Giraffe Academy.
-
My name is Mike.
-
In this tutorial, I
want to talk to you
-
guys about reading from
external files in Python.
-
Now, a lot of times
in Python, you're
-
going to want to read from files
that are outside of your Python
-
file.
-
So you might want
to read information
-
from a text file, or a
CSV file, or an HTML file.
-
And you can actually use
something called the Python read
-
command.
-
And it will allow you to
read a file that is stored
-
outside of your Python file.
-
So you can use these
files to get information,
-
or you can parse
through different files
-
and do different things.
-
So I'm going to show you guys
the basics of reading files,
-
opening files, closing files,
doing all of that stuff.
-
Over here, I have this
file called employees.txt.
-
And it basically just lists out
a bunch of different employees,
-
like these could be employees
in an office or whatever.
-
So it's just listing out
all of this information.
-
So let's say that
inside of my app
-
dot Python file, I wanted
to read the employees
-
inside of that file.
-
The first thing I
have to do is actually
-
open that file
from inside Python.
-
So I can use a special
command called open.
-
So I can say open.
-
And then in here, I can
type in the name of the file
-
that I want to open.
-
So this is either going
to be a relative path
-
of the file, an absolute
path to the file,
-
or just the file's name, if both
files are in the same directory.
-
So in my case, app dot
Python and employees.txt
-
are in the same folder, like
they're in the same directory.
-
So I can just type out
the name of the file.
-
I can just say employees.txt.
-
And then I want to put
one more thing inside
-
of this open function.
-
And it's going to be the mode
that I want to open the file in.
-
So you can actually open files
in a couple different modes.
-
And the first mode
is called read.
-
So I can just put an r here, and
that's going to stand for read.
-
And this basically
means that I only
-
want to read the
information inside the file.
-
I don't want to modify it.
-
I don't want to change it.
-
I just want to read it.
-
I just want to see
what's in the file
-
and do some stuff
with that information.
-
Another mode it's called write.
-
So I can type in this w.
-
And writing basically means
that you can change the file.
-
You can write new
information, you
-
can change existing information.
-
There's another one called a.
-
And a stands for append.
-
And this basically means that
you can append information
-
onto the end of the file.
-
So you can't modify any of
the information in the file.
-
You can't change any
of the information,
-
but you can add new information.
-
And there's one more,
which is r plus.
-
And this basically
means read and write.
-
So this will give you all the
power of reading and writing.
-
So in our case, we're just going
to be working with regular r.
-
So we're going to be
reading from the file.
-
Now this open function will
essentially just open the file.
-
So it's going to go over to that
file inside of our file system,
-
open it up, and it'll allow
us to read through it.
-
But generally, we're going to
want to store this opened file
-
inside of a variable.
-
So I can create a variable.
-
And we can just call
it employee_file.
-
And I'm just going to set it
equal to this open function.
-
So now the open employees.txt
file and all the content inside
-
of it is stored inside of
this employee file variable.
-
Now whenever you open
a file, you always
-
want to make sure that you
close the file as well.
-
So just like we have
this open command,
-
we also have an close function.
-
So I can come down here and
say employee_file.close.
-
And this is essentially just
going to close the file,
-
so we're no longer going
to be able to access it.
-
And generally, it's a good idea.
-
Whenever you're
opening up a file,
-
you want to also
make sure that you're
-
closing the file at some point.
-
So generally, once
you're done reading it,
-
you can just close it.
-
So that's how we can
open and close a file.
-
Now let's talk about how we can
get information from the file.
-
There's no point in having
the file there if we
-
can't figure out what's in it.
-
So there's actually a
few different functions
-
that we can use on
this employee file
-
to figure out
what's inside of it.
-
And I'm going to show
you guys some of those.
-
So I'm just going to
make a print statement.
-
And inside this print
statement, we'll
-
basically just print out some
information about the employee
-
file.
-
So the most basic
thing we can print out
-
is just the entire
contents of the file.
-
But before I do that,
I want to show you
-
guys how you can check to make
sure that a file is readable.
-
So before we do anything
else, generally, it's
-
a good idea to make sure that
it's possible to read this file.
-
And there's a function
inside of Python we can use,
-
called readable.
-
So I'm just going to type
out employee_file.readable.
-
-
And this is going to
return a Boolean value.
-
And it's going to
tell us whether or not
-
we can read from this file.
-
So I'm going to run my program.
-
And you'll see down here,
we get a value of true.
-
And that's because we set
the file with a read mode.
-
So it's in read mode,
we can read from it.
-
If I was to put a w
here, so if I put write,
-
now readable is
going to be false
-
because we can no
longer read the file,
-
we can only write to the file.
-
So I'm going to change this back
to r so we can just read it.
-
So once you've figured
out whether or not
-
the file can be read from,
let's actually read it.
-
So there's another function
called employee_file.read.
-
And this is basically just going
to spit out all the information
-
in the file.
-
So when I run the
program, it's just
-
going to spit out all of this
information, all the information
-
that was in that file.
-
I can also come
down here, and we
-
could read an individual
line inside this file.
-
So I can say
employee_file.read line.
-
And what this is
going to do is it's
-
going to read an individual
line inside of this file.
-
So now when I run
this program, you'll
-
see it's just reading that
first line in the file.
-
And this read line function
is actually just reading
-
the first line, and then it's
basically moving a little cursor
-
onto the next line.
-
So if I was to copy this line
of code and then print it again
-
down here, I'm saying
employee_file.readline.
-
So it's going to
read the first line.
-
And then when I
say it again, it's
-
going to read the
line after that.
-
So this is actually going to end
up printing out the first two
-
lines in the file.
-
So when I run this
program, you'll
-
see we print out Jim -
Salesman and Dwight - Salesman.
-
So if I was to do
this multiple times,
-
I could technically print out
every line inside of this file.
-
And you can see we can do that.
-
And so that can be pretty useful
for reading multiple lines
-
in a file.
-
But there's actually
another function
-
that is better at doing that.
-
And we can say, instead
of employee_file.readline,
-
we can say .readlines.
-
And what this is
going to do is it's
-
going to take all of the
lines inside of our file
-
and put them inside of an array.
-
And so now when
I print this out,
-
you'll see we have
this array down here.
-
It says Jim - Salesman, that's
the first item in the array.
-
Dwight - Salesman is the
second item in the array.
-
It's basically taking each
line and putting it inside
-
of an array.
-
So if I wanted to
access a specific line,
-
I can just refer to it by
its index in the array.
-
So if I said 1, now
this is going to give us
-
that Dwight - Salesman line.
-
Because that is at index
position 1 inside of the array.
-
You can also use this readlines
function with a for loop.
-
So I could come up here
and create a for loop.
-
I'm just going to
say for, and we'll
-
say employee in employee_file.
-
And then for each employee, we
just want to print them out.
-
And actually, sorry, we have to
put employee_file.readlines up
-
here.
-
And so now this will loop
through all of the employees
-
in this
employee_file.readlines array.
-
So we can actually just print
out the individual employee.
-
And now this will print
out all the employees
-
inside of that file.
-
So it's basically printing
out each line in the file.
-
And that can be pretty useful.
-
So you can use all of these
different functions read,
-
readline, readlines, readable.
-
There's a bunch of
these different things
-
that we can do to get
information from a file.
-
And so there's a
lot of cases where
-
you're going to want to be able
to parse through information
-
in a file.
-
And this is a
awesome way to do it.
-
So just to recap, whenever
you want to open a file
-
and read from it, you can
just use this open function,
-
type in the name of the file and
then the mode, which in our case
-
is going to be r.
-
Then you can do all
sorts of stuff with it.
-
And you always want to
make sure that you close it
-
when you're done.
-
That's just good practice.
-
So that's the basics
of reading from files.
-
And hopefully, you guys can
use this in some way, shape,
-
or form in the future.
-
Hey, Thanks for watching.
-
If you enjoyed the video,
please leave 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.