[Tutor] Corrupt files

Roger Merchberger zmerch at 30below.com
Tue Aug 2 22:56:25 CEST 2005

Rumor has it that geon may have mentioned these words:
>Øyvind napsal(a):
> >Hello.
> >
> >I have created a program that automatically downloads some files I need.
> >It can be .zip, .jpg, .mpg or .txt. However, a lot of the time urlretrieve
> >downloads the file, gives no error, but the downloaded file is corrupted.
> >I use a simple check for the size of the file. If it is way too small, I
> >automatically remove it.
> >
>remove it and try to download it again, lets say , 3times until you get
>correct copy...

Or, depending on it's extension, try to open the newly downloaded file with 
the corresponding Python extension, i.e. (.jpg|.gif|.png) try to open with 
PIL[1], .zip files with the python zipfile module, etc. I *think* there's 
modules for .mpgs and maybe .avi's... but .txt files might be tough to 
authenticate, as there's no actual 'file format.'

This python pseudocode might explain more:

=-=-=-=-=-=-= snip here =-=-=-=-=-=-=

def openzip(filename):
     import zipfile
       filebuf = zipfile.open(filename)
       return "Error 99: cannot open file; corrupted zipfile."

     return None

if __name__ == '__main__':

     if filename[:-4] == '.zip':
         retcode = openzip(filename)
         if retcode != None:
             print "Corrupt File - Try to download again!"

=-=-=-=-=-=-= snip here =-=-=-=-=-=-=

Just put an 'openzip' or 'openjpg' type of function for each type of file 
you'll need to authenticate.

[[ This code will *not* run -- it certainly won't run in Eudora (where it's 
being typed), so it's only for showing the general idea... YMMV, UAYOR, 
etc. ;-) ]]

Hope this helps,
Roger "Merch" Merchberger

[1] PIL == Python Image Library; it will open almost any type of static 
graphic image available.

Roger "Merch" Merchberger   | "Bugs of a feather flock together."
sysadmin, Iceberg Computers |           Russell Nelson
zmerch at 30below.com          |

More information about the Tutor mailing list