Active Topics

 


Reply
Thread Tools
BrentDC's Avatar
Posts: 903 | Thanked: 632 times | Joined on Apr 2008
#1
So I'm working on a fairly simple python script, but have run into a problem.

I'm trying to append data to a file, but the .append object doesn't work (.write does, though):

Code:
Traceback (most recent call last):
  File "/home/user/scripts/battery_check/battery_check.py", line 20, in <module>
    logfile.append(add_time)
AttributeError: 'file' object has no attribute 'append'
My code is this:

Code:
   logfile = open(date_stamp, 'w')
   logfile.append(add_time)
Where date_stamp and add_time are predefined variables.

Any idea what is wrong?
 
Posts: 1,101 | Thanked: 1,185 times | Joined on Aug 2008 @ Spain
#2
You should consult the python library reference.
file objects doesn't have the append method, only mutable secuence types (like lists) have .append
On files, you can open the file for appending, so writes get appended to the end of the file. Do not confuse the file on disk with a file object.
 
BrentDC's Avatar
Posts: 903 | Thanked: 632 times | Joined on Apr 2008
#3
Thanks. Yep, I read that somewhere, too. The "Dive into Python" text was a little confusing. I confuse easily
 
pycage's Avatar
Posts: 3,404 | Thanked: 4,474 times | Joined on Oct 2005 @ Germany
#4
Dive into Python is excellent work but really confusing stuff.

You can use mode "a" to append:
Code:
fd = open("myfile", "a")
fd.write("some text")
fd.close()
 

The Following User Says Thank You to pycage For This Useful Post:
BrentDC's Avatar
Posts: 903 | Thanked: 632 times | Joined on Apr 2008
#5
Originally Posted by pycage View Post
Dive into Python is excellent work but really confusing stuff.

You can use mode "a" to append:
Code:
fd = open("myfile", "a")
fd.write("some text")
fd.close()
Thanks a lot pycage! That worked great.

Oh, and I'm glad someone else thinks Dive into Python is confusing
 
Reply


 
Forum Jump


All times are GMT. The time now is 21:12.