emergent/swarm/evolutionary systems etc

Peter Hansen peter at engcorp.com
Fri Apr 2 07:03:11 EST 2004


Peter MacKenzie wrote:

> import sys

This part is useless for the four lines that follow, but
of course you'll need it if you ever want to access anything
that really is in the sys module...

> file = 'myfile.txt'

Here the name "file" is bound to a string.

> open(file,'r+b')

Here you call a builtin and pass it two strings.  Functions
can "never" modify what the arguments are bound to, so this
cannot modify the name "file" to something else.  Clearly not
what you intended.  Note that open() really *returns* the newly
created file object, so you should be assigning the result as:

file = open(filename, 'r+b')

Of course, now you need a "filename" name, which is what the
first line should be doing:

   filename = 'myfile.txt'  # instead of "file = ..."

> file.write("some string of what you want to write")
> file.close()

These two will now work.

Why don't you run through the Python tutorial first, since these
are pretty basic questions, some of which will be resolved if you
go the defined routes first.

There are also other good Python/newbie resources linked to from
the web site if you go there and poke around.

-Peter



More information about the Python-list mailing list