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.
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to