Re: [Tutor] why it is showing attribute error in line7

2016-10-29 Thread Steven D'Aprano
Hello, and welcome!

Please always post the FULL traceback. Python gives you lots of 
information to debug problems, so you don't have to guess, but when you 
throw that information away, we have to guess.

My guess follows below:

On Fri, Oct 28, 2016 at 09:42:35PM -0700, SONU KUMAR wrote:
> fname = raw_input("Enter file name: ")
> if len(fname) < 1 : fname = "mbox-short.txt"
> fh = open(fname)
> count = 0
> for line in fh:
>line=line.rstrip

That's the problem. You are not calling the method, instead you are 
assigning the method line.rstrip to the variable "line". Watch the 
difference here:

py> line = "some text"
py> a = line.rstrip()  # with parentheses means call the method
py> b = line.rstrip  # no parens means the method itself
py> print(a)
some text
py> print(b)



So using your code, you say:

py> line = line.rstrip  # no parens
py> print(line)



which means when you come to the next line of code, you get an error:

>if not line.startswith("From"):continue

py> line.startswith('From')
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: 'builtin_function_or_method' object has no attribute 
'startswith'


And look at the error message: it tells you *exactly* what is wrong. You 
have a built-in function or method, not a string.

Always read the full traceback. When you have to ask for help, always 
copy and paste the full traceback.



-- 
Steve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] why it is showing attribute error in line7

2016-10-29 Thread Alan Gauld via Tutor
Please always post the full error text in future.

Meanwhile I'll guess:


On 29/10/16 05:42, SONU KUMAR wrote:
> fname = raw_input("Enter file name: ")
> if len(fname) < 1 : fname = "mbox-short.txt"
> fh = open(fname)
> count = 0
> for line in fh:
>line=line.rstrip

missing parens means you reassign line to the method.

>if not line.startswith("From"):continue

The method does not have a startwith attribute.

>lst=line.split()

Nor a split method.

> 
>print lst[1]
> 
>count=count+1
> print "There were", count, "lines in the file with From as the first word"

HTH


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] why it is showing attribute error in line7

2016-10-29 Thread SONU KUMAR
fname = raw_input("Enter file name: ")
if len(fname) < 1 : fname = "mbox-short.txt"
fh = open(fname)
count = 0
for line in fh:
   line=line.rstrip
   if not line.startswith("From"):continue
   lst=line.split()

   print lst[1]

   count=count+1
print "There were", count, "lines in the file with From as the first word"
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor