AttributeError

Emile van Sebille emile at fenx.com
Wed Aug 12 19:15:14 EDT 2015


On 8/12/2015 4:05 PM, Ltc Hotspot wrote:
> On Wed, Aug 12, 2015 at 3:35 PM, MRAB <python at mrabarnett.plus.com> wrote:
>> On 2015-08-12 22:16, Denis McMahon wrote:
>> [snip]
>>
>>> c = [0 for i in range(24)]
>>> f = open(filename,'r')
>>> for l in f:
>>>       h = int(l.strip().split()[X].split(':')[Y])
>>>       c[h] = c[h] + 1
>>> f.close()
>>> for i in range(24):
>>>       print '{:02d} {}'.format(i, c[i])
>>>
>> There's no need to strip whitespace just before splitting on it.
>>
>> This:
>>
>>      l.strip().split()
>>
>> can be shortened to this:
>>
>>      l.split()
>>
>> --
>> https://mail.python.org/mailman/listinfo/python-list
>
>
> MRAB,
>
> The Revised code produces a traceback:
> ()
>        9
>       10 c = [0 for i in range(24)]
> ---> 11 f  = open(filename,'r')
>       12 for l in f:
>       13      h = int(l.split()[X].split(':')[Y])
>
> NameError: name 'filename' is not defined

So, read your posted code below and tell us where the label 'filename' 
is defined.  We can't find it.  Neither can python.  That's what the 
error message means.  Was that not clear?  You do understand the 
difference between text and labels, right?  You have by now reviewed the 
tutorial?

Emile


>
>
>
> The revised code reads:
>
> handle = """From stephen.marquard at uct.ac.za Sat Jan  5 09:14:16 2008
>  From louis at media.berkeley.edu Fri Jan  4 18:10:48 2008
> """.split("\n")
> # Snippet file data: mbox-short.txt
>
> count = dict()
> #fname = raw_input("Enter file name: ")# Add Snippet file
> #handle = open (fname, 'r')# Add Snippet file
>
> c = [0 for i in range(24)]
> f  = open(filename,'r')
> for l in f:
>       h = int(l.split()[X].split(':')[Y])
>       c[h] = c[h] + 1
> f.close()
> for i in range(24):
>       print '{:02d} {}'.format(i, c[i])
>
>
> How do I define the file name in order to remove the traceback?
>
> Regards,
> Hal
>





More information about the Python-list mailing list