Reading, writing files

seanm sean.marimpietri at gmail.com
Sat Aug 22 00:21:52 CEST 2009


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



More information about the Python-list mailing list