Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Reading, writing files

Reply
Thread Tools

Reading, writing files

 
 
seanm
Guest
Posts: n/a
 
      08-21-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
 
Reply With Quote
 
 
 
 
Albert Hopkins
Guest
Posts: n/a
 
      08-21-2009
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.
>



 
Reply With Quote
 
 
 
 
MRAB
Guest
Posts: n/a
 
      08-21-2009
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Files not writing, closing files, finalize() Matt Java 35 11-21-2008 07:49 AM
how i can extract text from the PDF files,power point files,Ms word files? crazyprakash Java 4 10-30-2005 10:17 AM
Text files read multiple files into single file, and then recreate the multiple files googlinggoogler@hotmail.com Python 4 02-13-2005 05:44 PM
Any problems with writing the information into a file - Multi-users perform writing the same file at the same time ???? HNguyen ASP .Net 4 12-21-2004 01:53 PM
Help on a strategy for writing files of aprox. 0.5mb to disk at arate of 30 files per second Ben Jeurissen C++ 5 02-03-2004 04:07 AM



Advertisments