user3164083
user3164083

Reputation: 1073

Overwrite text file on first write, then append to it - Python

My first write to the file needs to overwrite it, then my next ones need to append to it. But There is no way to know what write will be first. My writes are in conditional statements. Here is what I have:

class MyHTMLParser(HTMLParser):
    def __init__(self):
        HTMLParser.__init__(self)
        self.strict = False
        self.indent = " "
        self.pos = 0
        self.output_file = 'output_sass.txt'

    def handle_starttag(self, tag, attrs):
        if attrs != []:
            for attr in attrs:
                if ('id' in attr):
                    id = attr.index('id')
                    with open(self.output_file, 'w') as the_file:
                        the_file.writelines(self.indent * self.getpos()[1] + '#' + attr[id+1] + ' {' +'\n')
##                    print (self.indent * self.getpos()[1] + "#" + attr[id+1] + " {")
                    self.pos = self.getpos()[1]
                    break
                elif ('class' in attr):
                    clas = attr.index('class')
                    with open(self.output_file, 'w') as the_file:
                        the_file.writelines(self.indent * self.getpos()[1] + "." + attr[clas+1] + " {"+'\n')
##                    print (self.indent * self.getpos()[1] + "." + attr[clas+1] + " {")
                    self.pos = self.getpos()[1]
                    break
                else:
                    with open(self.output_file, 'w') as the_file:
                        the_file.writelines(self.indent * self.getpos()[1] + tag + " {"+'\n')
##                    print (self.indent * self.getpos()[1] + tag + " {")
                    self.pos = self.getpos()[1]
                    break
        else:
           with open(self.output_file, 'w') as the_file:
                the_file.writelines(self.indent * self.getpos()[1] + tag + " {"+'\n')
##            print (self.indent * self.getpos()[1] + tag + " {")
                self.pos = self.getpos()[1]

    def handle_endtag(self, tag):
        with open(self.output_file, 'w') as the_file:
            the_file.writelines(self.indent * self.pos + "}"+'\n')
##        print(self.indent * self.pos + "}")

Upvotes: 4

Views: 3993

Answers (2)

furas
furas

Reputation: 142651

use some variable first_write = True and check it in all places. Then change it to False.

Or put data on some list and write only once at the end.

Of course you can open file for writing once at the beginning (deleting previous file) and close it at the end.

In __init__ do self.the_file = open(self.output_file, 'w') and you have open file and you can access it in all class. I don't only know when will be end of program to close file self.the_file.close(). Maybe HTMLParser has some function called at the end of data.

See HTMLParser.close() - it seems good place to close file. You will have to overwrite it and probably call close() from oryginal class HTMLParser.

Upvotes: 1

inspectorG4dget
inspectorG4dget

Reputation: 113965

Add a class attribute that holds the changing flag:

import itertools

class MyHTMLParser(HTMLParser):
    def __init__(self, ...):
        ...
        self.modes = itertools.chain('w', itertools.cycle('a'))

    @property
    def mode(self):
        return next(self.modes)

    def handle_starttag:
        ...
        with open(filepath, self.mode) as the_file:  # self.mode is 'w' the first time and 'a' every time thereafter
            # write stuff

Upvotes: 2

Related Questions