file / string problem?

Clarence Gardner clarence at
Fri Mar 24 16:14:37 CET 2000

Your system being a MS-DOS descendent, you need to be sure to
open files in binary mode if they don't contain text.
    f = open('D:\\tmp\\esbdates.hlp', 'wb')
Also, with the code as given, if you get a write error, you won't
see it.

On Thu, 23 Mar 2000, Todd Palmer wrote:
>I new to python and I'm having a problem writing a binary string to a
>file.  I'm using the module and getting the data from the
>zip file and writing it to a file object.  The string returned from
>the function is the correct size, but when I write it
>to disk, the file on the disk is the wrong size and corrupted.  Any
>ideas?  Do I have to do something special with binary data in strings?
>import zipfile
>z = zipfile.ZipFile('D:\\tmp\\')
>	# read the binary data into a string
> 	 S ='esbdates.hlp')
>	# note len(S) here returns 362588 which is correct  as per 
>	# AND winzip
>	f = open('D:\\tmp\\esbdates.hlp', 'w')
>	try:
>		f.write(S)
>	finally:
>		f.close()
>		# file size is 365,452 bytes!
>	z.close()
>Python 1.5.2
>Platform: Win NT 4.0 SP 5  
>The more I learn, the less I know.
>This could take forever.
>Todd Palmer
>t2palmer at

More information about the Python-list mailing list