473,804 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sorta noob question - file vs. open?

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.tx t", "w")
MyFile.write("T his is a test.")
MyFile.close()

However, using:
MyFile = open("MyFile.tx t", "w")
MyFile.write("T his 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
Aug 23 '05 #1
8 1791
Peter A. Schott wrote:
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.tx t", "w")
MyFile.write("T his is a test.")
MyFile.close() But you've given us no idea what went wrong.
However, using:
MyFile = open("MyFile.tx t", "w")
MyFile.write("T his 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
Sc***********@A cm.Org
Aug 23 '05 #2
In article <6a************ *************** *****@4ax.com>,
Peter A. Schott <pa******@no.ya hoo.spamm.com> wrote:
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.tx t", "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
Aug 23 '05 #3
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....c7fbacf0866763
(which didn't start out as a file-or-open discussion, but there you
go).

Hope this helps,
Jason

Aug 23 '05 #4
Peter A.Schott wrote:
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.tx t", "w")
MyFile.write("T his is a test.")
MyFile.close()

However, using:
MyFile = open("MyFile.tx t", "w")
MyFile.write("T his 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.
open is file True


regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Aug 24 '05 #5
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/pyt...ly/045921.html
http://mail.python.org/pipermail/pyt...ly/045967.html
# Open file for writing, write something, close file
MyFile = file("MyFile.tx t", "w")
MyFile.write("T his 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.tx t", "w")
MyFile.write("T his is a test.")
MyFile.close()

I have no problems.


</F>

Aug 24 '05 #6
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

Scott David Daniels <Sc***********@ Acm.Org> wrote:
Peter A. Schott wrote:
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.tx t", "w")
MyFile.write("T his is a test.")
MyFile.close()

But you've given us no idea what went wrong.
However, using:
MyFile = open("MyFile.tx t", "w")
MyFile.write("T his 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
Sc***********@A cm.Org

Aug 24 '05 #7
Peter A. Schott wrote:
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
Sc***********@A cm.Org
Aug 24 '05 #8
Peter A. Schott wrote:
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.f ile')

if isinstance(outp utFile, open): ...

whereas these _are_ preferred:

outputFile = open('path.to.f ile')

if isinstance(outp utFile, file): ...

-Peter
Aug 24 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
1689
by: Igorati | last post by:
Ok, this is what I have so far: #This program will ask for a user to imput numbers. The numbers will then be calculated #to find the statistical mean, mode, and median. Finallly the user will be asked #if he would like to print out the answers. list = a = 1 print 'Enter numbers to add to the list. (Enter 0 to quit.)'
3
6082
by: We need more power captain | last post by:
Hi, I know less than a noob, I've been asked to do some compiles in VC++ 6 without knowing too much at all. (I'm a COBOL program normally so this is all too much for me) I open VC++6, open the workspace and then: Build>Batch Build
0
1688
by: haylow | last post by:
Hi I am new to ASP.NET and am working on an application that runs on a webserver. The user will open up the web interface in a browser on their local machine and enter a path to a directory. I have 2 problems: First, I can't find a control that will let the user browse the file system and specify a directory - the only one I can find is the one that lets the user specify a file.
8
1588
by: hiro | last post by:
Hi there, I'm very new to python, the problem I need to solve is whats the "best/ simplest/cleanest" way to read in multiple files (ascii), do stuff to them, and write them out(ascii). -- import os filePath = ('O:/spam/eggs/')
8
2038
by: sore eyes | last post by:
Hi I just downloaded the free Watcom compiler and am having a little trouble with File IO http://www.openwatcom.org/index.php/Download I downloaded the following example, commented out the Command line arguments so that I could debug more easily. The example is a simple file copy. and it works. but I would like to customize this to automate some redundant changes in some large files.
3
2333
by: gham | last post by:
I am a complete noob to linux and shell scripting please help this is what I am trying to do 1. Create a script that takes 1 argument being a file, read the inputted file, and look for occurrences of the current user who is executing the script. On finding an occurrence of the username take that line and append it to a file and display a line number and a bracket against the saved line efforts sofar #!/bin/bash echo "filename"
6
1635
by: Lang Murphy | last post by:
I'm baaaaack... some of you answered a question I had last week. Only problem is: I'm a dope who doesn't understand most of what y'all posted. Raw noob when it comes to .Net and C#. So I'm going to be more specific... All I need to do at the moment is figure out how to replace our usage of .ini files as configuration files for our small, utility type apps. I'm not looking to use XML as a data stream or anything like that. Not right now,...
6
1988
by: Paulchen | last post by:
Hello, I have found a perl script and need to "translate" this to PHP. I try to do it step by step and the first part of it is this function (the whole script is found at http://nonsense.sourceforge.net/): ### Load and parse a datafile, slurping the contents into the %pool hash sub LoadDataFile { my $file = shift; $file = SafeFile( $file ) if $cgi_mode; open IN, $file or die "Error opening $file... $!\n"; local $/ = ''; ...
4
2109
by: larry | last post by:
Ok I'm a Python noob, been doing OK so far, working on a data conversion program and want to create some character image files from an 8-bit ROM file. Creating the image I've got down, I open the file and use TK to draw the images... but 1) It does not seem to end (running in IDLE), I have to kill the process to retry it seems tkinter does not close(?)
0
9711
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9593
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10595
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9169
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5668
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4306
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.