Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #94791

Re: [Tutor] Mailbox

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!feeder1.xsusenet.com!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <cameron@cskk.homeip.net>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'subject:: [': 0.03; 'revision': 0.05; 'filename': 0.07; 'line:': 0.07; 'complaining': 0.09; 'defined.': 0.09; 'fname': 0.09; 'open()': 0.09; 'string)': 0.09; 'syntax': 0.13; 'file,': 0.15; '_after_': 0.16; 'count,': 0.16; 'from:addr:cs': 0.16; 'from:addr:zip.com.au': 0.16; 'from:name:cameron simpson': 0.16; 'last)': 0.16; 'len(line)': 0.16; 'message-id:@cskk.homeip.net': 0.16; 'runtime.': 0.16; 'simpson': 0.16; 'skip:> 50': 0.16; 'subject:Tutor': 0.16; 'word"': 0.16; 'wrote:': 0.16; 'subject:] ': 0.19; 'meant': 0.22; 'cheers,': 0.22; 'code,': 0.23; 'code.': 0.23; 'defined': 0.23; '(most': 0.24; 'header:In-Reply-To:1': 0.24; 'header:User- Agent:1': 0.26; 'order.': 0.27; 'loop,': 0.29; 'code:': 0.29; 'print': 0.30; 'error.': 0.31; 'supposed': 0.31; 'traceback': 0.33; 'open': 0.33; 'file': 0.34; 'but': 0.36; 'should': 0.36; 'lines': 0.36; 'to:addr:python-list': 0.36; 'charset:us-ascii': 0.37; 'list.': 0.37; 'starting': 0.37; 'things': 0.38; 'names': 0.38; 'to:addr:python.org': 0.40; 'your': 0.60; 'email addr:gmail.com': 0.62; 'cameron': 0.66; 'reply': 0.68; 'received:61': 0.72; "'from'": 0.84; '>if': 0.84; 'from"': 0.84; 'subject:Mailbox': 0.84
X-Authentication-Info Submitted using ID cskk@bigpond.com
X-Authority-Analysis v=2.0 cv=RsdH3VaK c=1 sm=1 a=c1mplxcSC4q7EqhOGsrsxA==:17 a=vrnE16BAAAAA:8 a=ZtCCktOnAAAA:8 a=yEdEr6MRgwAA:10 a=zOBTXjUuO1YA:10 a=pGLkceISAAAA:8 a=aFXScbbLQgOHBqBZLnkA:9 a=CjuIK1q_8ugA:10 a=c1mplxcSC4q7EqhOGsrsxA==:117
Date Fri, 31 Jul 2015 07:53:35 +1000
From Cameron Simpson <cs@zip.com.au>
To python-list@python.org
Subject Re: [Tutor] Mailbox
MIME-Version 1.0
Content-Type text/plain; charset=us-ascii; format=flowed
Content-Disposition inline
In-Reply-To <55ba7488.882c460a.ed373.ffff8098@mx.google.com>
User-Agent Mutt/1.5.23 (2014-03-12)
References <55ba7488.882c460a.ed373.ffff8098@mx.google.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1098.1438293232.3674.python-list@python.org> (permalink)
Lines 45
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1438293232 news.xs4all.nl 2853 [2001:888:2000:d::a6]:41534
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:94791

Show key headers only | View raw


On 30Jul2015 19:00, ltc.hotspot@gmail.com <ltc.hotspot@gmail.com> wrote:
>New revision code:
>
>count = 0
>fn = raw_input("Enter file name: ")
>if len(fn) < 1 : fname = "mbox-short.txt"
>for line in fn:
>  if 'From' in line.split()[0]: count += 1
>print "There are %d lines starting with From" % count
>print len(line)
>fn = open(fname)
>print "There were", count, "lines in the file with From as the first word"
>
>
>Syntax message produced by iPython interperter:
>
>NameError                                 Traceback (most recent call last)
>C:\Users\vm\Desktop\apps\docs\Python\assinment_8_5_v_2.py in <module>()
>      6 print "There are %d lines starting with From" % count
>      7 print len(line)
>----> 8 fn = open(fname)
>      9 print "There were", count, "lines in the file with From as the first wor
>d"
>
>NameError: name 'fname' is not defined

I have reposted this to the python-list. Please always reply to the list.

The message above is not a syntax error. It is a NameError, which happens at 
runtime.

It is complaining that the name "fname" is not defined.

This is because you have the names "fn" and "fname" confused in your code.  
Based on your code, "fname" is supposed to be the filename (a string) and "fn" 
is meant to be the open file object (a file), since you have this line:

  fn = open(fname)

However, you set "fn" from raw_input when you should set "fname". You also have 
the open() call _after_ your loop, but the loop needs to read from the open 
file, so these things are out of order.

Cheers,
Cameron Simpson <cs@zip.com.au>

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: [Tutor] Mailbox Cameron Simpson <cs@zip.com.au> - 2015-07-31 07:53 +1000

csiph-web