< Return to Video

Reading Files | Python | Tutorial 28

  • 0:00 - 0:02
    INSTRUCTOR: Hey, welcome
    to Giraffe Academy.
  • 0:02 - 0:02
    My name is Mike.
  • 0:02 - 0:04
    In this tutorial, I
    want to talk to you
  • 0:04 - 0:08
    guys about reading from
    external files in Python.
  • 0:08 - 0:10
    Now, a lot of times
    in Python, you're
  • 0:10 - 0:14
    going to want to read from files
    that are outside of your Python
  • 0:14 - 0:15
    file.
  • 0:15 - 0:17
    So you might want
    to read information
  • 0:17 - 0:21
    from a text file, or a
    CSV file, or an HTML file.
  • 0:21 - 0:25
    And you can actually use
    something called the Python read
  • 0:25 - 0:26
    command.
  • 0:26 - 0:30
    And it will allow you to
    read a file that is stored
  • 0:30 - 0:31
    outside of your Python file.
  • 0:31 - 0:34
    So you can use these
    files to get information,
  • 0:34 - 0:36
    or you can parse
    through different files
  • 0:36 - 0:37
    and do different things.
  • 0:37 - 0:40
    So I'm going to show you guys
    the basics of reading files,
  • 0:40 - 0:43
    opening files, closing files,
    doing all of that stuff.
  • 0:43 - 0:48
    Over here, I have this
    file called employees.txt.
  • 0:48 - 0:51
    And it basically just lists out
    a bunch of different employees,
  • 0:51 - 0:54
    like these could be employees
    in an office or whatever.
  • 0:54 - 0:58
    So it's just listing out
    all of this information.
  • 0:58 - 0:59
    So let's say that
    inside of my app
  • 0:59 - 1:04
    dot Python file, I wanted
    to read the employees
  • 1:04 - 1:05
    inside of that file.
  • 1:05 - 1:07
    The first thing I
    have to do is actually
  • 1:07 - 1:10
    open that file
    from inside Python.
  • 1:10 - 1:13
    So I can use a special
    command called open.
  • 1:13 - 1:15
    So I can say open.
  • 1:15 - 1:18
    And then in here, I can
    type in the name of the file
  • 1:18 - 1:19
    that I want to open.
  • 1:19 - 1:21
    So this is either going
    to be a relative path
  • 1:21 - 1:23
    of the file, an absolute
    path to the file,
  • 1:23 - 1:28
    or just the file's name, if both
    files are in the same directory.
  • 1:28 - 1:32
    So in my case, app dot
    Python and employees.txt
  • 1:32 - 1:35
    are in the same folder, like
    they're in the same directory.
  • 1:35 - 1:37
    So I can just type out
    the name of the file.
  • 1:37 - 1:40
    I can just say employees.txt.
  • 1:40 - 1:43
    And then I want to put
    one more thing inside
  • 1:43 - 1:45
    of this open function.
  • 1:45 - 1:48
    And it's going to be the mode
    that I want to open the file in.
  • 1:48 - 1:51
    So you can actually open files
    in a couple different modes.
  • 1:51 - 1:53
    And the first mode
    is called read.
  • 1:53 - 1:56
    So I can just put an r here, and
    that's going to stand for read.
  • 1:56 - 1:58
    And this basically
    means that I only
  • 1:58 - 2:01
    want to read the
    information inside the file.
  • 2:01 - 2:02
    I don't want to modify it.
  • 2:02 - 2:04
    I don't want to change it.
  • 2:04 - 2:05
    I just want to read it.
  • 2:05 - 2:07
    I just want to see
    what's in the file
  • 2:07 - 2:09
    and do some stuff
    with that information.
  • 2:09 - 2:11
    Another mode it's called write.
  • 2:11 - 2:13
    So I can type in this w.
  • 2:13 - 2:16
    And writing basically means
    that you can change the file.
  • 2:16 - 2:17
    You can write new
    information, you
  • 2:17 - 2:20
    can change existing information.
  • 2:20 - 2:22
    There's another one called a.
  • 2:22 - 2:24
    And a stands for append.
  • 2:24 - 2:27
    And this basically means that
    you can append information
  • 2:27 - 2:29
    onto the end of the file.
  • 2:29 - 2:31
    So you can't modify any of
    the information in the file.
  • 2:31 - 2:33
    You can't change any
    of the information,
  • 2:33 - 2:36
    but you can add new information.
  • 2:36 - 2:39
    And there's one more,
    which is r plus.
  • 2:39 - 2:41
    And this basically
    means read and write.
  • 2:41 - 2:45
    So this will give you all the
    power of reading and writing.
  • 2:45 - 2:49
    So in our case, we're just going
    to be working with regular r.
  • 2:49 - 2:52
    So we're going to be
    reading from the file.
  • 2:52 - 2:56
    Now this open function will
    essentially just open the file.
  • 2:56 - 2:59
    So it's going to go over to that
    file inside of our file system,
  • 2:59 - 3:02
    open it up, and it'll allow
    us to read through it.
  • 3:02 - 3:05
    But generally, we're going to
    want to store this opened file
  • 3:05 - 3:07
    inside of a variable.
  • 3:07 - 3:09
    So I can create a variable.
  • 3:09 - 3:12
    And we can just call
    it employee_file.
  • 3:12 - 3:16
    And I'm just going to set it
    equal to this open function.
  • 3:16 - 3:20
    So now the open employees.txt
    file and all the content inside
  • 3:20 - 3:25
    of it is stored inside of
    this employee file variable.
  • 3:25 - 3:27
    Now whenever you open
    a file, you always
  • 3:27 - 3:31
    want to make sure that you
    close the file as well.
  • 3:31 - 3:33
    So just like we have
    this open command,
  • 3:33 - 3:36
    we also have an close function.
  • 3:36 - 3:41
    So I can come down here and
    say employee_file.close.
  • 3:41 - 3:44
    And this is essentially just
    going to close the file,
  • 3:44 - 3:47
    so we're no longer going
    to be able to access it.
  • 3:47 - 3:49
    And generally, it's a good idea.
  • 3:49 - 3:51
    Whenever you're
    opening up a file,
  • 3:51 - 3:52
    you want to also
    make sure that you're
  • 3:52 - 3:54
    closing the file at some point.
  • 3:54 - 3:56
    So generally, once
    you're done reading it,
  • 3:56 - 3:57
    you can just close it.
  • 3:57 - 4:00
    So that's how we can
    open and close a file.
  • 4:00 - 4:03
    Now let's talk about how we can
    get information from the file.
  • 4:03 - 4:05
    There's no point in having
    the file there if we
  • 4:05 - 4:06
    can't figure out what's in it.
  • 4:06 - 4:09
    So there's actually a
    few different functions
  • 4:09 - 4:11
    that we can use on
    this employee file
  • 4:11 - 4:13
    to figure out
    what's inside of it.
  • 4:13 - 4:15
    And I'm going to show
    you guys some of those.
  • 4:15 - 4:17
    So I'm just going to
    make a print statement.
  • 4:17 - 4:19
    And inside this print
    statement, we'll
  • 4:19 - 4:22
    basically just print out some
    information about the employee
  • 4:22 - 4:23
    file.
  • 4:23 - 4:25
    So the most basic
    thing we can print out
  • 4:25 - 4:28
    is just the entire
    contents of the file.
  • 4:28 - 4:30
    But before I do that,
    I want to show you
  • 4:30 - 4:34
    guys how you can check to make
    sure that a file is readable.
  • 4:34 - 4:36
    So before we do anything
    else, generally, it's
  • 4:36 - 4:40
    a good idea to make sure that
    it's possible to read this file.
  • 4:40 - 4:43
    And there's a function
    inside of Python we can use,
  • 4:43 - 4:44
    called readable.
  • 4:44 - 4:48
    So I'm just going to type
    out employee_file.readable.
  • 4:48 - 4:51
  • 4:51 - 4:53
    And this is going to
    return a Boolean value.
  • 4:53 - 4:55
    And it's going to
    tell us whether or not
  • 4:55 - 4:57
    we can read from this file.
  • 4:57 - 4:58
    So I'm going to run my program.
  • 4:58 - 5:02
    And you'll see down here,
    we get a value of true.
  • 5:02 - 5:06
    And that's because we set
    the file with a read mode.
  • 5:06 - 5:08
    So it's in read mode,
    we can read from it.
  • 5:08 - 5:11
    If I was to put a w
    here, so if I put write,
  • 5:11 - 5:13
    now readable is
    going to be false
  • 5:13 - 5:15
    because we can no
    longer read the file,
  • 5:15 - 5:17
    we can only write to the file.
  • 5:17 - 5:22
    So I'm going to change this back
    to r so we can just read it.
  • 5:22 - 5:23
    So once you've figured
    out whether or not
  • 5:23 - 5:27
    the file can be read from,
    let's actually read it.
  • 5:27 - 5:30
    So there's another function
    called employee_file.read.
  • 5:30 - 5:33
    And this is basically just going
    to spit out all the information
  • 5:33 - 5:34
    in the file.
  • 5:34 - 5:36
    So when I run the
    program, it's just
  • 5:36 - 5:40
    going to spit out all of this
    information, all the information
  • 5:40 - 5:41
    that was in that file.
  • 5:41 - 5:43
    I can also come
    down here, and we
  • 5:43 - 5:45
    could read an individual
    line inside this file.
  • 5:45 - 5:48
    So I can say
    employee_file.read line.
  • 5:48 - 5:50
    And what this is
    going to do is it's
  • 5:50 - 5:54
    going to read an individual
    line inside of this file.
  • 5:54 - 5:56
    So now when I run
    this program, you'll
  • 5:56 - 5:59
    see it's just reading that
    first line in the file.
  • 5:59 - 6:02
    And this read line function
    is actually just reading
  • 6:02 - 6:05
    the first line, and then it's
    basically moving a little cursor
  • 6:05 - 6:06
    onto the next line.
  • 6:06 - 6:11
    So if I was to copy this line
    of code and then print it again
  • 6:11 - 6:15
    down here, I'm saying
    employee_file.readline.
  • 6:15 - 6:16
    So it's going to
    read the first line.
  • 6:16 - 6:18
    And then when I
    say it again, it's
  • 6:18 - 6:20
    going to read the
    line after that.
  • 6:20 - 6:22
    So this is actually going to end
    up printing out the first two
  • 6:22 - 6:24
    lines in the file.
  • 6:24 - 6:25
    So when I run this
    program, you'll
  • 6:25 - 6:29
    see we print out Jim -
    Salesman and Dwight - Salesman.
  • 6:29 - 6:31
    So if I was to do
    this multiple times,
  • 6:31 - 6:37
    I could technically print out
    every line inside of this file.
  • 6:37 - 6:39
    And you can see we can do that.
  • 6:39 - 6:41
    And so that can be pretty useful
    for reading multiple lines
  • 6:41 - 6:42
    in a file.
  • 6:42 - 6:44
    But there's actually
    another function
  • 6:44 - 6:45
    that is better at doing that.
  • 6:45 - 6:48
    And we can say, instead
    of employee_file.readline,
  • 6:48 - 6:50
    we can say .readlines.
  • 6:50 - 6:52
    And what this is
    going to do is it's
  • 6:52 - 6:55
    going to take all of the
    lines inside of our file
  • 6:55 - 6:57
    and put them inside of an array.
  • 6:57 - 6:58
    And so now when
    I print this out,
  • 6:58 - 7:00
    you'll see we have
    this array down here.
  • 7:00 - 7:04
    It says Jim - Salesman, that's
    the first item in the array.
  • 7:04 - 7:07
    Dwight - Salesman is the
    second item in the array.
  • 7:07 - 7:09
    It's basically taking each
    line and putting it inside
  • 7:09 - 7:10
    of an array.
  • 7:10 - 7:13
    So if I wanted to
    access a specific line,
  • 7:13 - 7:15
    I can just refer to it by
    its index in the array.
  • 7:15 - 7:18
    So if I said 1, now
    this is going to give us
  • 7:18 - 7:20
    that Dwight - Salesman line.
  • 7:20 - 7:24
    Because that is at index
    position 1 inside of the array.
  • 7:24 - 7:28
    You can also use this readlines
    function with a for loop.
  • 7:28 - 7:30
    So I could come up here
    and create a for loop.
  • 7:30 - 7:32
    I'm just going to
    say for, and we'll
  • 7:32 - 7:37
    say employee in employee_file.
  • 7:37 - 7:41
    And then for each employee, we
    just want to print them out.
  • 7:41 - 7:45
    And actually, sorry, we have to
    put employee_file.readlines up
  • 7:45 - 7:46
    here.
  • 7:46 - 7:49
    And so now this will loop
    through all of the employees
  • 7:49 - 7:51
    in this
    employee_file.readlines array.
  • 7:51 - 7:56
    So we can actually just print
    out the individual employee.
  • 7:56 - 7:58
    And now this will print
    out all the employees
  • 7:58 - 7:59
    inside of that file.
  • 7:59 - 8:03
    So it's basically printing
    out each line in the file.
  • 8:03 - 8:04
    And that can be pretty useful.
  • 8:04 - 8:07
    So you can use all of these
    different functions read,
  • 8:07 - 8:10
    readline, readlines, readable.
  • 8:10 - 8:12
    There's a bunch of
    these different things
  • 8:12 - 8:14
    that we can do to get
    information from a file.
  • 8:14 - 8:16
    And so there's a
    lot of cases where
  • 8:16 - 8:18
    you're going to want to be able
    to parse through information
  • 8:18 - 8:19
    in a file.
  • 8:19 - 8:21
    And this is a
    awesome way to do it.
  • 8:21 - 8:24
    So just to recap, whenever
    you want to open a file
  • 8:24 - 8:26
    and read from it, you can
    just use this open function,
  • 8:26 - 8:29
    type in the name of the file and
    then the mode, which in our case
  • 8:29 - 8:30
    is going to be r.
  • 8:30 - 8:32
    Then you can do all
    sorts of stuff with it.
  • 8:32 - 8:35
    And you always want to
    make sure that you close it
  • 8:35 - 8:35
    when you're done.
  • 8:35 - 8:37
    That's just good practice.
  • 8:37 - 8:40
    So that's the basics
    of reading from files.
  • 8:40 - 8:42
    And hopefully, you guys can
    use this in some way, shape,
  • 8:42 - 8:44
    or form in the future.
  • 8:44 - 8:45
    Hey, Thanks for watching.
  • 8:45 - 8:47
    If you enjoyed the video,
    please leave and subscribe
  • 8:47 - 8:49
    to Giraffe Academy to
    be the first to know
  • 8:49 - 8:50
    when we release new content.
  • 8:50 - 8:52
    Also, we're always
    looking to improve,
  • 8:52 - 8:55
    so if you have any constructive
    criticism, or questions,
  • 8:55 - 8:57
    or anything, leave
    a comment below.
  • 8:57 - 8:59
    Finally, if you're enjoying
    Giraffe Academy and you want
  • 8:59 - 9:03
    to help us grow, head over to
    giraffeacademy.com/contribute
  • 9:03 - 9:05
    and invest in our future.
Title:
Reading Files | Python | Tutorial 28
Description:

more » « less
Video Language:
English
Duration:
09:05

English subtitles

Revisions