Reading, writing files

Discussion in 'Python' started by seanm, Aug 21, 2009.

  1. seanm

    seanm Guest

    In the book I am using, they give the following function as an
    example:

    def copyFile(oldFile, newFile):
    f1 = open(oldFile, 'r')
    f2 = open(newFile, 'w')
    while True:
    text = f1.read(50)
    if text == "":
    break
    f2.write(text)
    f1.close()
    f2.close()
    return

    My question is why does this function successfully copy a 200
    character file, oldFile, to newFile? The line of code that reads, text
    = f1.read(50), does not seem to be iterative in any way to me. How is
    this fuction succeding in adding each additional set up 50 characters
    to the previous set of 50 characters read from oldFile?

    How does it even succeed in copying a 25 character file? If oldFile
    contains 25 characters, how does the program ever break out of the
    'while True:' loop?

    I just don't see it.

    Again, much thanks to anyone who can clear this up.

    -Sean
     
    seanm, Aug 21, 2009
    #1
    1. Advertising

  2. On Fri, 2009-08-21 at 15:21 -0700, seanm wrote:
    > In the book I am using, they give the following function as an
    > example:
    >
    > def copyFile(oldFile, newFile):
    > f1 = open(oldFile, 'r')
    > f2 = open(newFile, 'w')
    > while True:
    > text = f1.read(50)
    > if text == "":
    > break
    > f2.write(text)
    > f1.close()
    > f2.close()
    > return
    >
    > My question is why does this function successfully copy a 200
    > character file, oldFile, to newFile? The line of code that reads, text
    > = f1.read(50), does not seem to be iterative in any way to me. How is
    > this fuction succeding in adding each additional set up 50 characters
    > to the previous set of 50 characters read from oldFile?


    The body of the loop will execute forever (unless cut short by the
    "break" statement. What the loop is essentially doing is reading 50
    bytes at a time from f1 and writing it into f2. When f1 reaches end of
    file it will stop returning bytes (if text == "":) the loop is broken
    and both files are closed.

    > How does it even succeed in copying a 25 character file? If oldFile
    > contains 25 characters, how does the program ever break out of the
    > 'while True:' loop?
    >
    > I just don't see it.


    Have you read the documentation for file objects?

    During the first iteration of the loop 25 bytes are read from f1. Then
    they are written to f2. During the next iteration there is nothing else
    to be read from f1 so f1.read(50) returns "", which causes the loop to
    break.

    > Again, much thanks to anyone who can clear this up.
    >
     
    Albert Hopkins, Aug 21, 2009
    #2
    1. Advertising

  3. seanm

    MRAB Guest

    seanm wrote:
    > In the book I am using, they give the following function as an
    > example:
    >
    > def copyFile(oldFile, newFile):
    > f1 = open(oldFile, 'r')
    > f2 = open(newFile, 'w')
    > while True:
    > text = f1.read(50)


    This will read up to 50 characters from the input file. At the end of
    the file it'll return an empty string (""). In fact, the only time it'll
    return an empty string is at the end of the file.

    > if text == "":
    > break
    > f2.write(text)
    > f1.close()
    > f2.close()
    > return
    >
    > My question is why does this function successfully copy a 200
    > character file, oldFile, to newFile? The line of code that reads, text
    > = f1.read(50), does not seem to be iterative in any way to me. How is
    > this fuction succeding in adding each additional set up 50 characters
    > to the previous set of 50 characters read from oldFile?
    >
    > How does it even succeed in copying a 25 character file? If oldFile
    > contains 25 characters, how does the program ever break out of the
    > 'while True:' loop?
    >
    > I just don't see it.
    >
    > Again, much thanks to anyone who can clear this up.
    >

    It reads some characters from the input file and then writes then to the
    output file, and because of the 'while' loop it'll repeat action that
    until the read returns an empty string, which will be when it has read
    all the way to the end of file and tries to read some more.
     
    MRAB, Aug 21, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Modukuri
    Replies:
    0
    Views:
    2,923
    Modukuri
    May 19, 2004
  2. Daniel Gowans

    Reading/Writing pure binary files

    Daniel Gowans, May 27, 2004, in forum: VHDL
    Replies:
    2
    Views:
    5,223
  3. TC
    Replies:
    3
    Views:
    26,210
    jessica
    May 19, 2004
  4. Stephen
    Replies:
    1
    Views:
    677
    Kevin Spencer
    Mar 16, 2006
  5. Alex Polite
    Replies:
    17
    Views:
    750
    lyallex
    Jun 8, 2004
Loading...

Share This Page