opening a file opens it in Notepad
Author |
Message |
Lanc #1 / 8
|
opening a file opens it in Notepad
Hi All, I'm running PythonWin build 148 on Windows XP. The following results in the file being opened in Notepad! This confuses me greatly. I simply want the file handle. My next line is to read the file into a string, using f1.read() Any suggestions will be appreciated. Also, I have a number of files in srcfiles. I imagine I must issue f1.close() each time in the loop, yes? for file in srcfiles: f1 = open(file,"r") ... do stuff... Thanks, Lance
|
Mon, 25 Jul 2005 13:55:44 GMT |
|
|
Alex Martell #2 / 8
|
opening a file opens it in Notepad
Quote:
> Hi All, > I'm running PythonWin build 148 on Windows XP. > The following results in the file being opened in Notepad! This confuses > me greatly. I simply want the file handle. My next line is to read the > file into a string, using f1.read() > Any suggestions will be appreciated.
You may have a from whoknows import * somewhere which is {*filter*}ling the built-in name 'open' (or less likely some other way to rebind that built-in name). Forget about the existence of the 'from' statement and always use 'import' -- a rather extreme suggestion, but I think that for most people following it would enhance their code quality. Quote: > Also, I have a number of files in srcfiles. I imagine I must issue > f1.close() each time in the loop, yes?
It's cleaner style, though in current CPython it's not strictly necessary (but it *IS* in Jython and may become so in some future CPython, and it's a very good thing to get into the habit of so doing: "explicit is better than implicit"). Alex
|
Mon, 25 Jul 2005 17:01:41 GMT |
|
|
Anton Muhi #3 / 8
|
opening a file opens it in Notepad
Quote:
> Hi All, > I'm running PythonWin build 148 on Windows XP. > The following results in the file being opened in Notepad! This confuses me > greatly. I simply want the file handle. My next line is to read the file > into a string, using f1.read() > Any suggestions will be appreciated. > Also, I have a number of files in srcfiles. I imagine I must issue > f1.close() each time in the loop, yes? > for file in srcfiles: > f1 = open(file,"r") > ... do stuff... > Thanks, > Lance
Copy-paste of your code revealed no problem, pelase send the full script. BTW, using file as a variable is really, really bad --- file is a built-in function. Your code would be better written like this: for fileName in srcfiles: f = file(fileName, "r") # file is prefered to open now HTH, Anton
|
Mon, 25 Jul 2005 20:04:16 GMT |
|
|
Simon Brunnin #4 / 8
|
opening a file opens it in Notepad
Quote:
> I'm running PythonWin build 148 on Windows XP. > The following results in the file being opened in Notepad! This confuses > me > greatly. I simply want the file handle. My next line is to read the file > into a string, using f1.read() > Any suggestions will be appreciated. > Also, I have a number of files in srcfiles. I imagine I must issue > f1.close() each time in the loop, yes? > for file in srcfiles: > f1 = open(file,"r") > ... do stuff...
You don't have: from webbrowser import * lying around anywhere do you, or something like it? This smells like namespace pollution to me - i.e., you have accidentally imported another 'open' function over the top of the existing one. The 'from module import *' form is dangerous, and should be used judiciously. I never use it at all. Re closing your files - you don't *have* to close your files, but it's a good idea. The current C implementation of python will close your files for you as soon as you drop the reference to them, but this is not guaranteed behaviour - Jython doesn't immediately close files, and future versions of C Python conceivably might not. Cheers, Simon Brunning TriSystems Ltd.
----------------------------------------------------------------------- The information in this email is confidential and may be legally privileged. It is intended solely for the addressee. Access to this email by anyone else is unauthorised. If you are not the intended recipient, any disclosure, copying, distribution, or any action taken or omitted to be taken in reliance on it, is prohibited and may be unlawful. TriSystems Ltd. cannot accept liability for statements made which are clearly the senders own.
|
Mon, 25 Jul 2005 18:22:59 GMT |
|
|
Peter Hanse #5 / 8
|
opening a file opens it in Notepad
Quote:
> > for file in srcfiles: > > f1 = open(file,"r") > > ... do stuff... > Copy-paste of your code revealed no problem, pelase send the full > script. BTW, using file as a variable is really, really bad --- file is > a built-in function.
Well, there's no need to be that extreme about it. One might say using "file" as a name is "poor style", or perhaps merely "a bad habit", but it's not likely (I think) to do anything more than create a local variable which hides the global name "file", and that local variable is of course not going to pollute the namespace of any other code which relies on the builtin "file" binding. I suspect there is lots and lots of code out there that innocently uses variables named "input" or maybe "map", "hash", or "id" and I doubt that many problems result. But you're right that it is probably a good thing to learn to avoid... -Peter
|
Mon, 25 Jul 2005 21:40:10 GMT |
|
|
Anton Muhi #6 / 8
|
opening a file opens it in Notepad
Quote:
>>Copy-paste of your code revealed no problem, pelase send the full >>script. BTW, using file as a variable is really, really bad --- file is >>a built-in function. > Well, there's no need to be that extreme about it. One might > say using "file" as a name is "poor style", or perhaps merely "a bad > habit", but it's not likely (I think) to do anything more than > create a local variable which hides the global name "file", and > that local variable is of course not going to pollute the namespace > of any other code which relies on the builtin "file" binding. > -Peter
Sure, I overemphasized it :( Anton.
|
Mon, 25 Jul 2005 21:45:51 GMT |
|
|
Alex Martell #7 / 8
|
opening a file opens it in Notepad
... Quote: > I suspect there is lots and lots of code out there that innocently > uses variables named "input" or maybe "map", "hash", or "id" and
Oh yes. Quote: > I doubt that many problems result.
You may not have taught many beginners, perhaps. In my experience, there IS a substantial amount of time wasted because a beginner tries some nice construct he's been suggested or has read about and gets weird errors -- because he or she has unwittingly bound in some previous part of his or her code some built-in name that is used in the construct in question -- and a lesser but (I'd guess) still nontrivial amount of time similarly wasted by non-beginners. Some builtin names (those you might be better advised to never or VERY seldom use, like 'input') may be less troublesome than others in this respect, of course. Perhaps the worst are the name of popular types -- file, list, dict, tuple, int... -- too frequently rebound to mean "this here file", "yonder list" and so on, yet at the same time frequently needed on their own. It's a good thing that all of these are non-callable, since the typical use of a typename is calling it; so, the TypeError gets raised pretty soon. Still, if PyChecker could warn about such rebindings, it wouldn't be a bad thing, I believe. Quote: > But you're right that it is probably a good thing to learn to avoid...
So it's not a bad idea to keep reminding beginners not to do it... without overstating one's case, of course. Alex
|
Mon, 25 Jul 2005 22:03:59 GMT |
|
|
Lanc #8 / 8
|
opening a file opens it in Notepad
Thanks all for the solution.....indeed I had "from webbrowser import open" in my code.... Lance
Quote: > Hi All, > I'm running PythonWin build 148 on Windows XP. > The following results in the file being opened in Notepad! This confuses me > greatly. I simply want the file handle. My next line is to read the file > into a string, using f1.read() > Any suggestions will be appreciated. > Also, I have a number of files in srcfiles. I imagine I must issue > f1.close() each time in the loop, yes? > for file in srcfiles: > f1 = open(file,"r") > ... do stuff... > Thanks, > Lance
|
Mon, 25 Jul 2005 22:34:37 GMT |
|
|
|