maemo.org - Talk

maemo.org - Talk (https://talk.maemo.org/index.php)
-   Development (https://talk.maemo.org/forumdisplay.php?f=13)
-   -   Python .append problem -- help needed (https://talk.maemo.org/showthread.php?t=26204)

BrentDC 2009-01-14 01:20

Python .append problem -- help needed
 
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?

maacruz 2009-01-14 01:56

Re: Python .append problem -- help needed
 
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 2009-01-14 02:15

Re: Python .append problem -- help needed
 
Thanks. Yep, I read that somewhere, too. The "Dive into Python" text was a little confusing. I confuse easily :o

pycage 2009-01-14 08:41

Re: Python .append problem -- help needed
 
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()


BrentDC 2009-01-14 19:48

Re: Python .append problem -- help needed
 
Quote:

Originally Posted by pycage (Post 257455)
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 :o


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

vBulletin® Version 3.8.8