On 7/30/2015 4:41 PM, [email protected] wrote:
Hi Everyone:
Why is open not defined in the following code:NameError: name 'open' is not
defined
Because of something you did previously.
We don't have enough information to answer. open exists as a built-in
function in python:
Python 2.5 (r25:51908, Dec 18 2009, 14:22:21)
[GCC 3.4.6 20060404 (Red Hat 3.4.6-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> open
<built-in function open>
We'd need to see a full cut-n-paste example to help diagnose what's
actually happening. I can get a NameError with:
>>> del open
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'open' is not defined
>>>
So, don't try to delete built-in identifiers.
For example, when I paste your code from below into a python shell, I get:
Python 2.5 (r25:51908, Dec 18 2009, 14:22:21)
[GCC 3.4.6 20060404 (Red Hat 3.4.6-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> fname = raw_input("Enter file name: ")
Enter file name: if len(fname) < 1 : fname = "mbox-short.txt"
>>> fh = open(fname)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IOError: [Errno 2] No such file or directory: 'if len(fname) < 1 : fname
= "mbox-short.txt"'
>>> count = 0
>>> for line in fh:
Please paste in the contents from your screen where the error appears.
Note that in my example the open line complains about fname not existing
so I'm getting and IOError, and if the issue were with open, as per the
example with xxopen below, you then get the NameError.
>>> fh = xxopen('yyy')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'xxopen' is not defined
Please post the equivalent from your system and we can help better.
Emile
Code reads as follows:
fname = raw_input("Enter file name: ")
if len(fname) < 1 : fname = "mbox-short.txt"
fh = open(fname)
count = 0
for line in fh:
if not line.startswith('From'): continue
line2 = line.strip()
line3 = line2.split()
line4 = line3[1]
print line4
count = count + 1
print "There were", count, "lines in the file with From as the first word"
Regards,
Hal
Sent from Surface
_______________________________________________
Tutor maillist - [email protected]
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
_______________________________________________
Tutor maillist - [email protected]
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor