Discussion:
[Tutor] 'open' is not defined
l***@gmail.com
2015-07-30 23:41:56 UTC
Permalink
Hi Everyone:


Why is open not defined in the following code:NameError: name 'open' is not defined


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 - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Emile van Sebille
2015-07-31 01:01:27 UTC
Permalink
On 7/30/2015 4:41 PM, ***@gmail.com 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 - ***@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
l***@gmail.com
2015-07-31 01:13:34 UTC
Permalink
Hi Emile,


I hope this answers your question?


Question: How do I remove each duplicate line output?







Here is the raw data code:




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"




The problem is in the output results:




Python 2.7.10 |Anaconda 2.3.0 (64-bit)| (default, May 28 2015, 16:44:52)

[MSC v.1500 64 bit (AMD64)]
Type "copyright", "credits" or "license" for more information.




IPython 3.2.0 -- An enhanced Interactive Python.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.




In [1]: cd C:\Users\vm\Desktop\apps\docs\Python
C:\Users\vm\Desktop\apps\docs\Python




In [2]: %run _8_5_v_7.py
Enter file name: mbox-short.txt
***@uct.ac.za
***@uct.ac.za
***@media.berkeley.edu
***@media.berkeley.edu
***@umich.edu
***@umich.edu
***@iupui.edu
***@iupui.edu
***@umich.edu
***@umich.edu
***@iupui.edu
***@iupui.edu
***@iupui.edu
***@iupui.edu
***@iupui.edu
***@iupui.edu
***@umich.edu
***@umich.edu
***@umich.edu
***@umich.edu
***@umich.edu
***@umich.edu
***@umich.edu
***@umich.edu
***@iupui.edu
***@iupui.edu
***@umich.edu
***@umich.edu
***@caret.cam.ac.uk
***@caret.cam.ac.uk
***@gmail.com
***@gmail.com
***@uct.ac.za
***@uct.ac.za
***@uct.ac.za
***@uct.ac.za
***@uct.ac.za
***@uct.ac.za
***@uct.ac.za
***@uct.ac.za
***@uct.ac.za
***@uct.ac.za
***@media.berkeley.edu
***@media.berkeley.edu
***@media.berkeley.edu
***@media.berkeley.edu
***@media.berkeley.edu
***@media.berkeley.edu
***@iupui.edu
***@iupui.edu
***@iupui.edu
***@iupui.edu
***@iupui.edu
***@iupui.edu
There were 54 lines in the file with From as the first word




In [3]:









Regards,

Hal
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Steven D'Aprano
2015-07-31 02:23:14 UTC
Permalink
On Thu, Jul 30, 2015 at 11:41:56PM +0000, ***@gmail.com wrote:
> Hi Everyone:
>
>
> Why is open not defined in the following code:NameError: name 'open' is not defined

Are you still running your code on the PythonTutor website?

http://pythontutor.com/visualize.html

says in the fine-print at the bottom of the page:

"Online Python Tutor currently supports five languages (despite
its name!):

1. Python 2.7 and 3.3 with limited module imports and no file I/O."


So open() is not defined because the Online Python Tutor website has
removed it.

By the way, I tried working with that website for a little while. It is
*amazingly* slow and clunky and painful to use on my computer. I don't
know how you can bear to use it. Have you considered installing Python
on your computer and working directly on that?



--
Steve
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Continue reading on narkive:
Loading...