EOF

  • Thread starter Anjanesh Lekshminarayanan
  • Start date
A

Anjanesh Lekshminarayanan

|Hi

Im trying to download a file from a server. But how do I detect EOF ?
||


import urllib2

f1 = urllib2.urlopen('ftp://username:p[email protected]/data.zip')
f2 = file("data.zip", "wb")

while f1: # When to stop ?
f2.write(f1.read(1024))

f1.close()
f2.close()

||

I can get the size & use it in a for loop :

||||length = float(f1.info().getheader("Content-Length"))
block = 1024

for i in range(0, int(length/block + 1)):
s = f1.read(block)
f2.write(s)|||


But this will not work for a file whose size is not known. So I need to
get this done via EOF method.

Thanks
 
E

Eric Wertman

Im trying to download a file from a server. But how do I detect EOF ?

Shouldn't this work as well?

f1 = urllib2.urlopen('ftp://username:p[email protected]/data.zip')
f2 = file("data.zip", "wb")

while f1: # When to stop ?
try :
f2.write(f1.read(1024))
except EOFError :
break

f1.close()
f2.close()

Not that it's really any better, if it works... I'm wondering myself.

Eric
 
G

Gabriel Genellina

Whenever read() method returns empty string/list.


retval = f1.read()
if not retval:
break
f2.write(retval)

Those read() should be read(size) - read() tries to get the whole contents in memory.
Anyway, shutil.copyfileobj already implements that logic.
 
A

Anjanesh Lekshminarayanan

Thanks for the shutil.copyfileobj.
Oddly, the EOFError didnt work though.
 

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,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top