[Tutor] creating files with python and a thanks
Sheila King
sheila@thinkspot.net
Sun, 17 Feb 2002 20:46:18 -0800
On Mon, 18 Feb 2002 03:12:55 +0000, Eve Kotyk <e.kotyk@shaw.ca> wrote
about Re: [Tutor] creating files with python and a thanks:
> > What Python Shell are you using? What operating system?
>
> I'm using version 1.5.2 on Linux.
Well, I'm really flabbergasted. I just logged into my web host, where I
have a shell account, and they have 1.5.2 on Linux, but it worked for me
there, as well:
Python 1.5.2 (#1, Dec 21 2000, 15:29:08) [GCC egcs-2.91.66
19990314/Linux (egcs - on linux-i386
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> def gather_data():
... items = raw_input("Add food item: ")
... portion = input("Enter number of portions: ")
... cal_portion = input("Enter calories per portion: ")
... output = [items, portion, cal_portion]
... f = open('myfile.txt', 'w')
... f.write(str(output))
... f.close()
...
>>> gather_data()
Add food item: cumquats
Enter number of portions: 10
Enter calories per portion: 50
>>>
Let's look at that error message you showed before:
Traceback (innermost last):
File "<pyshell#78>", line 1, in ?
gather_data()
File "<pyshell#75>", line 9, in gather_data
f.write(output)
TypeError: read-only buffer, list
It says:
File "<pyshell#75>", line 9, in gather_data
f.write(output)
TypeError: read-only buffer, list
Notice how it seems to think that your write statement is
f.write(output)
and not f.write(str(output))
For some reason, it has an old copy of the gather_data function in
memory, I'm thinking.
Try closing down the interpreter and starting up a new session and type
everything over again and see if it doesn't work this time.
(Sorry, I didn't catch the importance of that error message previously.)
--
Sheila King
http://www.thinkspot.net/sheila/
"When introducing your puppy to an adult cat,
restrain the puppy, not the cat." -- Gwen Bailey,
_The Perfect Puppy: How to Raise a Well-behaved Dog_