[Tutor] creating files with python and a thanks
Eve Kotyk
e.kotyk@shaw.ca
Mon, 18 Feb 2002 13:14:27 +0000
Sheila King wrote:
>
> 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.
You are exactly right! I just tried it this morning and it worked very
well. I did not know that the interpreter would keep a previous bit of
code in memory.
>
> (Sorry, I didn't catch the importance of that error message previously.)
No problem but could you explain the error message a little? I assumed
it meant that I had variable elements of a different type (such as a
string and integers) but I tried the code again using only strings and I
still got the same error. What is a read-only buffer?
E
--
ekotyk
http://members.shaw.ca/e.kotyk/virtualstudio.htm