append log file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tom

    append log file

    Hi,
    I have a log file and of course I want to add the new information to the
    end of that log file. Unfortunately I always delete the old information
    and only append the current info.

    Right now I do it like this:
    I call a module which consists of a class and a function. It actually
    doesn't matter, but I just included it for the sake of completeness.
    The module looks like this:

    class log_C:
    def errorlog(self, filename, Data, d):
    LogFile = file(filename, 'w')
    ErrorInfo = Data[d+1]
    LogFile.write(E rrorInfo)
    LogFile.close()

    How can I add the new info without deleting the old info?
    I tried the modes 'a' and 'a+' but they both didn't work. Actually the
    data was totally unreadable! It was kind of messed up. :-( I don't know why.

    Thanks for your help.
    Regards, Tom

  • AK

    #2
    Re: append log file

    In article <bla2rc$4b3$1@n ews.uni-kl.de>, Tom wrote:[color=blue]
    > Hi,
    > I have a log file and of course I want to add the new information to the
    > end of that log file. Unfortunately I always delete the old information
    > and only append the current info.
    >
    > Right now I do it like this:
    > I call a module which consists of a class and a function. It actually
    > doesn't matter, but I just included it for the sake of completeness.
    > The module looks like this:
    >
    > class log_C:
    > def errorlog(self, filename, Data, d):
    > LogFile = file(filename, 'w')[/color]

    Why not ... open(filename ... ? Then 'a' should work. (or at least works
    here).
    [color=blue]
    > ErrorInfo = Data[d+1]
    > LogFile.write(E rrorInfo)
    > LogFile.close()
    >
    > How can I add the new info without deleting the old info?
    > I tried the modes 'a' and 'a+' but they both didn't work. Actually the
    > data was totally unreadable! It was kind of messed up. :-( I don't know why.
    >
    > Thanks for your help.
    > Regards, Tom[/color]

    [color=blue]
    >[/color]

    Comment

    • Gary Herron

      #3
      Re: append log file

      [color=blue]
      > Why not ... open(filename ... ? Then 'a' should work. (or at least works
      > here).[/color]

      The new and prefered way to open a file is to use "file()". The old
      "open()" is a synonym kept around for backwards compatibility.

      Gary Herron




      Comment

      • Gary Herron

        #4
        Re: append log file

        On Monday 29 September 2003 12:53 pm, Tom wrote:[color=blue]
        > Hi,
        > I have a log file and of course I want to add the new information to the
        > end of that log file. Unfortunately I always delete the old information
        > and only append the current info.
        >
        > Right now I do it like this:
        > I call a module which consists of a class and a function. It actually
        > doesn't matter, but I just included it for the sake of completeness.
        > The module looks like this:
        >
        > class log_C:
        > def errorlog(self, filename, Data, d):
        > LogFile = file(filename, 'w')
        > ErrorInfo = Data[d+1]
        > LogFile.write(E rrorInfo)
        > LogFile.close()
        >
        > How can I add the new info without deleting the old info?
        > I tried the modes 'a' and 'a+' but they both didn't work. Actually the
        > data was totally unreadable! It was kind of messed up. :-( I don't know
        > why.[/color]

        Well ... A mode of "a" is the correct thing, abd it works just fine
        for me. So we need to know some other things to solve this problem.

        What platform?

        Is the data ascii or binary? (If binary on win32, then you probably
        need mode "ab" or some such)

        Exactly what do you mean by "messed up"?

        Gary Herron



        Comment

        • Gerrit Holl

          #5
          Re: append log file

          Tom wrote:[color=blue]
          > I have a log file and of course I want to add the new information to the
          > end of that log file. Unfortunately I always delete the old information
          > and only append the current info.[/color]
          [color=blue]
          > LogFile = file(filename, 'w')[/color]

          You should use 'a' instead of 'w'. 'a' means 'append'. 'w' always means
          overwriting the content.
          [color=blue]
          > How can I add the new info without deleting the old info?
          > I tried the modes 'a' and 'a+' but they both didn't work. Actually the
          > data was totally unreadable! It was kind of messed up. :-( I don't know why.[/color]

          What did you try with 'a' exactly? It should work!

          Gerrit.

          --
          128. If a man take a woman to wife, but have no intercourse with her,
          this woman is no wife to him.
          -- 1780 BC, Hammurabi, Code of Law
          --
          Asperger Syndroom - een persoonlijke benadering:

          Het zijn tijden om je zelf met politiek te bemoeien:
          De website van de Socialistische Partij (SP) in Nederland: Informatie, nieuws, agenda en publicaties.


          Comment

          • Tom

            #6
            Re: append log file

            Gary Herron wrote:
            [color=blue]
            >Well ... A mode of "a" is the correct thing, abd it works just fine
            >for me. So we need to know some other things to solve this problem.
            >
            >What platform?
            >
            >[/color]
            I am using windows.
            [color=blue]
            >Is the data ascii or binary? (If binary on win32, then you probably
            >need mode "ab" or some such)
            >
            >[/color]
            It is just ascii.
            [color=blue]
            >Exactly what do you mean by "messed up"?
            >[/color]
            The Data in the log file looks like this:
            ??0/000/000/000/000/000/00?0/00?0/000/000/000/000/000/000/000/000/00?
            Instead it should look like this:
            1 1 0 0 0 0 0 0 1 0 1
            [color=blue]
            >Gary Herron
            >[/color]
            I am confused.
            -Tom

            Comment

            • Gary Herron

              #7
              Re: append log file

              On Monday 29 September 2003 02:19 pm, Tom wrote:[color=blue]
              > Gary Herron wrote:[color=green]
              > >Well ... A mode of "a" is the correct thing, abd it works just fine
              > >for me. So we need to know some other things to solve this problem.
              > >
              > >What platform?[/color]
              >
              > I am using windows.
              >[color=green]
              > >Is the data ascii or binary? (If binary on win32, then you probably
              > >need mode "ab" or some such)[/color]
              >
              > It is just ascii.
              >[color=green]
              > >Exactly what do you mean by "messed up"?[/color]
              >
              > The Data in the log file looks like this:
              > ??0/000/000/000/000/000/00?0/00?0/000/000/000/000/000/000/000/000/00?
              > Instead it should look like this:
              > 1 1 0 0 0 0 0 0 1 0 1
              >[color=green]
              > >Gary Herron[/color]
              >
              > I am confused.
              > -Tom[/color]

              I don't believe it is ascii. The line (shown above) is extremely
              garbled, but it looks like a binary (or unicode) string garbled as it
              passes through you mail client while sending and my mail client while
              reading.

              So do this:
              print type(ErrorInfo)
              and
              print `ErrorInfo` # the back quotes are important here
              before you write it out and tell us the results.

              I think you'll find that the contents of ErrorInfo is not a string
              filled with only printable ascii characters.

              Gary Herron




              Comment

              • Dennis Lee Bieber

                #8
                Re: append log file

                Tom fed this fish to the penguins on Monday 29 September 2003 02:19 pm:


                [color=blue][color=green]
                >>[/color]
                > The Data in the log file looks like this:
                > ??0/000/000/000/000/000/00?0/00?0/000/000/000/000/000/000/000/000/00?
                > Instead it should look like this:
                > 1 1 0 0 0 0 0 0 1 0 1
                >[/color]
                Looks like something trying to render binary data in some printable
                encoding (is that what you see if you type the file to screen, or what
                you see if you open it in Notepad/Wordpad)

                Your Data[d+1] is suspicious too... What is Data, an array of strings?
                (and why the +1, since Python indices start with 0). If Data is NOT an
                array of strings, you probably need to convert the contents to
                printable representation (maybe repr(ErrorInfo) or str(ErrorInfo))


                As for the opening... If "a" by itself is not working, maybe "a+"
                followed by a seek to end-of-file will do the trick.

                --[color=blue]
                > =============== =============== =============== =============== == <
                > wlfraed@ix.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
                > wulfraed@dm.net | Bestiaria Support Staff <
                > =============== =============== =============== =============== == <
                > Bestiaria Home Page: http://www.beastie.dm.net/ <
                > Home Page: http://www.dm.net/~wulfraed/ <[/color]

                Comment

                • Tom

                  #9
                  Re: append log file

                  Gary Herron wrote:
                  [color=blue]
                  >I don't believe it is ascii. The line (shown above) is extremely
                  >garbled, but it looks like a binary (or unicode) string garbled as it
                  >passes through you mail client while sending and my mail client while
                  >reading.
                  >
                  >[/color]
                  Now it starts getting really weird. :-)
                  If I append my error info once to my log file, I can read it. If I
                  append the same info twice to my log file it is garbled up like I
                  discribed earlier. If I append it three times, I can rad it again. Well
                  this is something I absolutely don't understand anymore. :-)
                  But it also looks like it is not neccessarily a python problem, because
                  I also found out, that I only have problems viewing it if I view it with
                  the Windows Text Editor. Then I tried to view it with Ultra Edit because
                  I knew that it should work! And with Ultra Edit everything looked fine!
                  I didn't expect these kind of problems with the windows editor because I
                  never had problems before, but it looks like this is the source of the
                  problem. Even though I don't understand why it just sometimes has this
                  problem.
                  [color=blue]
                  >So do this:
                  > print type(ErrorInfo)
                  >and
                  > print `ErrorInfo` # the back quotes are important here
                  >before you write it out and tell us the results.
                  >
                  >[/color]
                  It is a string and the print out looks exactly like the one I want and
                  sometimes even get. It consists of '0', '1', '2' and 'whitespace' only
                  [color=blue]
                  >I think you'll find that the contents of ErrorInfo is not a string
                  >filled with only printable ascii characters.
                  >
                  >Gary Herron
                  >[/color]
                  Thanky for your help,
                  Tom

                  Comment

                  Working...