Sorta noob question - file vs. open?

P

Peter A. Schott

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

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

However, using:
MyFile = open("MyFile.txt", "w")
MyFile.write("This is a test.")
MyFile.close()

I have no problems.

I'm sure that I'm missing something here about using open vs file, but am not
sure what. Probably just mis-read something. If anyone can point me to what I
didn't quite get, I'd appreciate the information.

Thanks.

-Pete
 
S

Scott David Daniels

Peter said:
Been reading the docs saying that file should replace open in our code, but this
doesn't seem to work:
This was really a misstatement; open is still preferred. file is now
a built in class, and its constructor is the same as open. I think the
current docs have been fixed.
# Open file for writing, write something, close file
MyFile = file("MyFile.txt", "w")
MyFile.write("This is a test.")
MyFile.close()
But you've given us no idea what went wrong.
However, using:
MyFile = open("MyFile.txt", "w")
MyFile.write("This is a test.")
MyFile.close()
I cannot help you if you don't tell me what went wrong.

Both bits of code seem to do the same thing for me.


--Scott David Daniels
(e-mail address removed)
 
R

Russell E. Owen

Peter A. Schott said:
Been reading the docs saying that file should replace open in our code, but
this
doesn't seem to work:

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

This should work in a sufficiently recent version of python (I'm hedging
because I don't recall when file was introduced -- python 2.2? 2.3?).
What version are you using? What error do you see?

-- Russell
 
J

Jason Drew

Both your code snippets above work should work OK. If it seems like a
file isn't being written, maybe you should specify its full path so you
are sure about where to check for it.

On the file-or-open question, the Python docs state, "The intent is for
open() to continue to be preferred for use as a factory function which
returns a new file object."

I happen to know that because it was clarified for me recently by a few
posters in this informative thread:
http://groups.google.com/group/comp.lang.python/browse_frm/thread/fbc7fbacf0866763
(which didn't start out as a file-or-open discussion, but there you
go).

Hope this helps,
Jason
 
S

Steve Holden

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

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

However, using:
MyFile = open("MyFile.txt", "w")
MyFile.write("This is a test.")
MyFile.close()

I have no problems.

I'm sure that I'm missing something here about using open vs file, but am not
sure what. Probably just mis-read something. If anyone can point me to what I
didn't quite get, I'd appreciate the information.

Thanks.

-Pete

I can only assume you are using an older version of Python:

$ python
Python 2.4.1 (#1, May 27 2005, 18:02:40)
[GCC 3.3.3 (cygwin special)] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
regards
Steve
 
F

Fredrik Lundh

Peter said:
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>
 
P

Peter A. Schott

I'll have to try this again. I obviously did something wrong in my code. I was
getting errors about not being able to write a string because it wasn't
supported. It was driving me nuts for a while until I just gave up and went
back to open(). I'll do some more playing and if I continue to get errors, I'll
post the code I'm using at the time along with any errors I got.

I apologize for not including the details. A thread I was reading reminded me
about that odd behaviour from a couple of weeks ago and I wanted to see if I was
way off base as I could consistently reproduce it then on Python 2.4.1.

Thanks to all who replied. If open is still preferred, I will stick with that.
:)

-Pete
 
S

Scott David Daniels

Peter said:
I'll have to try this again. I obviously did something wrong in my code. I was
getting errors about not being able to write a string because it wasn't
supported. It was driving me nuts for a while until I just gave up and went
back to open().

I expect somewhere previously in the same frame (perhaps the
interpreter top level) you had done either "file = open(...)"
or "file = file(...)" which would prevent you from getting to the
file in __builtins__.

--Scott David Daniels
(e-mail address removed)
 
P

Peter Hansen

Peter said:
Thanks to all who replied. If open is still preferred, I will
> stick with that.

FWIW, that's not an unqualified "preferred". To demonstrate by example,
neither of the above is considered preferred, though they both work:

outputFile = file('path.to.file')

if isinstance(outputFile, open): ...

whereas these _are_ preferred:

outputFile = open('path.to.file')

if isinstance(outputFile, file): ...

-Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top