[Zope] FW: Writing to a file

Thomas B. Passin tpassin@mitretek.org
Fri, 30 Nov 2001 13:44:21 -0500


If you want to write files to the file system, you probably want to use an
external python method.  It wouldn't be hard, but of course it takes a bit
of python programming.  But there are some issues to take care of:

1) With your proposed algorithm, you could be opening a lot of files, all of
which may stay open until near the end of the outer loops.  Not only does
this make you vulnerable in case of problems (many open files waiting to be
affected), but you could run out of available file handles on some systems.

So you should see if you can change your algorithm design to avoid these
problems.

2) You are presumably planning to write to files named for the various banks
in some way.  If a second user started a similar report before the work was
finished for a previous user, some of the files could be overwritten, and
there is no guarantee that the results would be as desired for the first
user.  Alternatively, one of the users could get an locked file exception
and wouldn't end up getting their results.

You need to consider how to handle such concurrent uses.

3) You should find a way to avoid including the absolute paths to the files
in your code.  My preferred approach is to locate them relative to the code
module that processes them.  You can ask the module to tell you where it is
in the file system, and construct the final paths using that information.

This lets you move the code around to different places on different machines
and have it still work.

You could also use an environmental variable to point to the file locations,
but then you have to arrange a way to set it and not forget to do so when
you move the system to a different computer.

4) Use the path functions in the python os.path library module to construct
paths rather than hard-coding path separators like "\".  This will allow you
to move between Windows and Unix machines without having to change any code.

Cheers,

Tom P

[Beaudette, Sheree]

> I'm going to resubmit my question below since I haven't yet received any
> answer.  Is there anyone who can at least lead me in the right direction?
> I'm sure people have done this before.  Still new to Zope and especially
> Python.
>
> Thanks!
>
> > -----Original Message-----
> > From: Beaudette, Sheree
> > Sent: Thursday, November 29, 2001 12:12 PM
> > To: 'zope@zope.org'
> > Subject: Writing to a file
> >
> > The logic here is simple but I'm not sure about the code.  Also not sure
> > how or if I need to use Python (which I'm just learning).
> >
> > I'm in a zope dtml method called cldmSearchForm
> >
> > what I need to do from this method is:
> >
> > start a loop that loops through the SQL method clsqBanks until there are
> > no more banks
> > open a file which will be named something differently for each bank
(i.e.
> > bkbankidrom.txt)
> > write a header of some sort
> > start another loop which loops through the SQL method clsqOfficers until
> > there are no more officers
> > for each officer write the bankno, officer no, etc, then new line
> > end officer loop
> > close file
> > end bank loop
> >