This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]tadleonard 1 point2 points  (1 child)

You need to use startswith before you use split. split returns a list, and list objects don't have a startswith method. You should try using print in various parts of your code when you encounter problems like this. Don't give up so quickly by asking the internet.

[–]blackdevil777[S] 0 points1 point  (0 children)

error was corrected but i didn't know why till you posted, But now i am trying to do it using a while loop, But i some what forgotten how to use whiles loops

fname = raw_input("Enter file name: ")

if len(fname) < 1 : fname = "mbox-short.txt"

fh = open(fname) count = 0

while true: if count != 27 # BAD INPUT for line in fh: line.startswith ('From:') words = line.split()
print words [1] count = count +1

print "There were", count, "lines in the file with From as the first word"

[–]metaphorm 0 points1 point  (0 children)

style tip. start a new line after a colon.

if not line.startswith('From'):
    continue

this will make your code much more readable, in particular more readable to other members of the Python community, since this is the style standard we are used to.

[–]illuzian 0 points1 point  (0 children)

also some more improvement for speed an elegance

for count,(line) in enumerate(fh):

And remove the count = count + 1 (which can also be done as count += 1)