Path: csiph.com!optima2.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!border1.nntp.ams1.giganews.com!nntp.giganews.com!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; "subject:' ": 0.07; 'fname': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:not': 0.11; 'count,': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'word"': 0.16; 'typical': 0.18; 'defined': 0.23; 'module': 0.25; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'print': 0.30; 'code': 0.30; 'open': 0.33; 'file': 0.34; 'execution': 0.35; 'follows:': 0.35; '(and': 0.36; 'to:addr:python-list': 0.36; 'received:org': 0.37; 'charset:us-ascii': 0.37; 'why': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'received:de': 0.40; 'received:217': 0.66 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: dieter Subject: Re: 'open' is not defined Date: Fri, 31 Jul 2015 08:22:46 +0200 References: <55babad0.c25e460a.2b9e.ffffb3cd@mx.google.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Gmane-NNTP-Posting-Host: pd9e08f91.dip0.t-ipconnect.de User-Agent: Gnus/5.1008 (Gnus v5.10.8) XEmacs/21.4.22 (linux) Cancel-Lock: sha1:4sH9oMoD7OVD/3fIUrlPjxlV54E= X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 23 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1438323776 news.xs4all.nl 2955 [2001:888:2000:d::a6]:38448 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:94799 writes: > ... > 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" You might execute the code above in a "restricted execution environment" (one where "__builtins__" is not the typical "__builtin__" module -- that's how "open" (and other builtins) is found).