file / string problem?

Todd Palmer t2palmer at bellsouth.net
Fri Mar 24 20:33:59 EST 2000


On Fri, 24 Mar 2000 07:14:37 -0800, Clarence Gardner
<clarence at netlojix.com> wrote:

>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')

Thanks, that  worked.  Missed the 'b' mode in the docs.

>Also, with the code as given, if you get a write error, you won't
>see it.

As I understand it, this is not true.  The finally will execute, but
the exception will still be raised.  To test this, I wrote:

import zipfile

### BEGIN CODE
z = zipfile.ZipFile('D:\\tmp\\esbdates.zip')
try:
	# read the binary data into a string
	S = z.read('ESBDates.hlp')
	# note len(S) here returns 362588 which is correct  as per
zipfile.py AND winzip
	f = open('D:\\tmp\\esbdates.hlp', 'wb')
	try:
		f.write(S)
		raise Exception
	finally:
		f.close()
finally:
	z.close()

## END CODE

and I get:

[d:\tpalmer\python]$ ziptest.py
Traceback (innermost last):
  File "D:\tpalmer\Python\ziptest.py", line 11, in ?
    raise Exception
Exception

So the exception is still raised.  I am not catching the exception in
a try..except block, I am just ensuring that the resources (file &
zipfile objects) are closed.

todd
--
The more I learn, the less I know.
This could take forever.

Todd Palmer
Developer
t2palmer at avana.net



More information about the Python-list mailing list