< Return to Video

Writing to Files | Python | Tutorial 29

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

more » « less
Video Language:
English
Duration:
07:07

English subtitles

Revisions