Sorta noob question - file vs. open?

Fredrik Lundh fredrik at pythonware.com
Wed Aug 24 01:20:48 EDT 2005


Peter A.Schott wrote:

> Been reading the docs saying that file should replace open in our code, 
> but this
> doesn't seem to work:

what docs?

"open" is the preferred way to open a file.  "file" is a type constructor.
in contemporary python, they happen to map to the same callable, but
that's not necessarily something that will also apply to future releases.
see:

    http://mail.python.org/pipermail/python-dev/2004-July/045921.html
    http://mail.python.org/pipermail/python-dev/2004-July/045967.html

> # Open file for writing, write something, close file
> MyFile = file("MyFile.txt", "w")
> MyFile.write("This is a test.")
> MyFile.close()

instead of saying that something "doesn't seem to work", it's always
more helpful to explain what happens.  do you get a traceback?  what
does the traceback say?

> However, using:
> MyFile = open("MyFile.txt", "w")
> MyFile.write("This is a test.")
> MyFile.close()
>
> I have no problems.

</F> 






More information about the Python-list mailing list