Reading, writing files

S

seanm

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
 
A

Albert Hopkins

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.
 
M

MRAB

seanm said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top