[Tutor] creating files with python and a thanks
Sheila King
sheila@thinkspot.net
Sun, 17 Feb 2002 18:42:25 -0800
On Sun, 17 Feb 2002 21:48:16 +0000, Eve Kotyk <e.kotyk@shaw.ca> wrote
about Re: [Tutor] creating files with python and a thanks:
> Thank you. This is very helpful, but I'm still not quite there. Here
> are my results from an Interpreter session.
>
> >>>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]
> #return 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
> 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
I have no idea why it is giving you difficulty. It works for me:
Python 2.2 (#28, Dec 21 2001, 12:21:22) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
IDLE 0.8 -- press F1 for help
>>> 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
>>>
What Python Shell are you using? What operating system?
--
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_