[Tutor] Attribute error?
Alan Gauld
alan.gauld at blueyonder.co.uk
Mon Dec 1 23:52:59 EST 2003
> uid = raw_input("Please enter your userID: ")
> gid = raw_input("Please enter your gID: ")
> f = open("file.txt","a")
> f.append("\nYour userID is " + uid + " And your gID is " + gid)
> -----
>
> but when I input the values while executing it, it gives me an
error:
>
> -----
> Traceback (most recent call last):
> File "./test2.py", line 6, in ?
> f.append("\nYour userID is " + uid + " And your gID is "
+ gid)
> AttributeError: 'file' object has no attribute 'append'
> -----
>
> but I already used "a" for it to append.. I don't get it =(
Thats right, you told Python that when you *write* to the file
it should append the data o the existing file. But the file
object doesn't have a method called append, you just write to it.
f.writeline(".............)
And you should really close the file after use - its a good
habit even if Python will do it for you at the end of the
program.
f.close()
Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld
More information about the Tutor
mailing list