append log file

T

Tom

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(ErrorInfo)
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
 
A

AK

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')

Why not ... open(filename ... ? Then 'a' should work. (or at least works
here).
ErrorInfo = Data[d+1]
LogFile.write(ErrorInfo)
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
 
G

Gary Herron

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(ErrorInfo)
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.

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
 
G

Gary Herron

Why not ... open(filename ... ? Then 'a' should work. (or at least works
here).

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
 
G

Gerrit Holl

Tom said:
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.
LogFile = file(filename, 'w')

You should use 'a' instead of 'w'. 'a' means 'append'. 'w' always means
overwriting the content.
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.

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

Gerrit.
 
T

Tom

Gary said:
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?
I am using windows.
Is the data ascii or binary? (If binary on win32, then you probably
need mode "ab" or some such)
It is just ascii.
Exactly what do you mean by "messed up"?
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
Gary Herron
I am confused.
-Tom
 
G

Gary Herron

I am using windows.


It is just ascii.


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


I am confused.
-Tom

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
 
D

Dennis Lee Bieber

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


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
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.

--
 
T

Tom

Gary said:
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.
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.
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.
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
I think you'll find that the contents of ErrorInfo is not a string
filled with only printable ascii characters.

Gary Herron
Thanky for your help,
Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top