<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <title></title>
</head>
<body>
I have a suggestion also though i am not an expert in python.  I have tried
to solve the problem like this: as i have taken a dictionary and i am inserting
the filenames which i am creating dynamically. And if you open the file for
the first time use the 'w' mode otherwise insert the filename into the dictionary
and use the 'a' mode. This way you will automatically delete the file when
you create it for the first time because we are using write mode for the
first time. <br>
<br>
class XYZ:<br>
        def __init__(self):<br>
                self.fileList = {}<br>
                self.key = 0<br>
        <br>
        <br>
<br>
        def writefile(self,data):<br>
                filename = '%s-%s.txt' % (fontname,tag)<br>
<br>
        #Check whether this file has already been opened or not.<br>
        <br>
                if filename in self.fileList.values():<br>
                        mode = 'a'<br>
                else:<br>
                        self.fileList[self.key] = filename<br>
                        mode = 'w'<br>
                        self.key = self.key+1<br>
<br>
                outfile = file(filename, mode)<br>
                outfile.write(data)<br>
                outfile.close() <br>
<br>
<br>
I hope i am right!!!<br>
Suggestions from tutor will make it more suitable for you to use.<br>
<br>
<br>
Thanks a lot.<br>
<br>
Regards,<br>
<br>
Anish <br>
<br>
Alan Gauld wrote:<br>
<blockquote type="cite" cite="mid025401c377e9$fb39f270$6401a8c0@xp">
  <blockquote type="cite">
    <pre wrap="">i.e. append mode so that i can append information to the file while
    </pre>
  </blockquote>
  <pre wrap=""><!---->i am
  </pre>
  <blockquote type="cite">
    <pre wrap="">running the program.
    </pre>
  </blockquote>
  <pre wrap=""><!---->
Yes thats the right approach.

  </pre>
  <blockquote type="cite">
    <pre wrap="">and three files are generated

a.txt, b.txt, c.txt


Now suppose if i run the program again then i get into problem
    </pre>
  </blockquote>
  <pre wrap=""><!---->because now
  </pre>
  <blockquote type="cite">
    <pre wrap="">again these three files will be appending the information in the
    </pre>
  </blockquote>
  <pre wrap=""><!---->existing
  </pre>
  <blockquote type="cite">
    <pre wrap="">files which i created last time i run the program.
    </pre>
  </blockquote>
  <pre wrap=""><!---->
OK so you need to add a tst for file existence before opening it.
Then if it already exists (and the datestamp is within T seconds
of the current time) then use append othewise use 'w' which will
overwrite the old version.

Alternatively do some housekeeping first and copy the old file to
a .bak before creating the new one.

Put the test in a function:

def alreadyExists(fname):
    # test here

then your code looks like:

if alreadyExists(fname): mode = 'a'
else: mode = 'w'
f = open(fname,mode)

HTH,

Alan g


_______________________________________________
Tutor maillist  -  <a class="moz-txt-link-abbreviated" href="mailto:Tutor@python.org">Tutor@python.org</a>
<a class="moz-txt-link-freetext" href="http://mail.python.org/mailman/listinfo/tutor">http://mail.python.org/mailman/listinfo/tutor</a>


  </pre>
</blockquote>
<br>
</body>
</html>