ZIP files

Alex Martelli aleaxit at yahoo.com
Fri Nov 12 06:12:19 EST 2004


Thomas Heller <theller at python.net> wrote:
   ...
> > def write_data(filename, data, flags='w'):
> >     fileobject = open(filename, flags)
> >     try: fileobject.write(data)
> >     finally: fileobject.close()
> >
> > needs to be coded once, and then makes the operation just as convenient
> > as doing it inline...
> 
> I'd suggest to expand this a bit, and make it working correctly on
> windows too, where binary files must be opened with the 'b' flag:

Hmmm, I thought the optional flags parm would suffice, but you're
probably right it wouldn't...

> def _write_data(filename, data, flags):
>     fileobject = open(filename, flags)
>     try: fileobject.write(data)
>     finally: fileobject.close()
> 
> def write_data(filename, data, flags="wb"):
>     _write_data(filename, data, flags)
> 
> def write_text(filename, text, flags="w"):
>     _write_data(filename, data, flags)
> 
> plus the corresponding read_data() and read_text() functions.
> Hm, add an encoding for unicode, maybe...
> Cookbook recipe, or standard lib?

I'm tempted to add convenience to the write_data wrapper by accepting
some non-str type for data, but that might be going overboard...


Alex



More information about the Python-list mailing list