0

Is it possible to read from a certain line onwards? In the example I cited below, I had want to read and use only from Line04 onwards

with open (fileList[0], 'rt') as filehandle:
     for line in filehandle:
         print line

# Output:
# This is a testing file
#
# v 1.05
# v -2.15

3 Answers 3

2

You can just skip over the first four lines, using enumerate to count them:

with open(fileList[0], 'rt') as filehandle:
    for line_num, line in enumerate(filehandle):
        if line_num < 4:
            continue

        print line
        # and do anything else
Sign up to request clarification or add additional context in comments.

1 Comment

I guess this is the best way. Was looking for a way to use seek, but that isn't going to work.
1

This should work:

with open('your_file', 'rt') as filehandle:
  lines = filehandle.readlines()[4:]

for line in lines:
    print line
    #do something

Comments

1
lineno = 0
for line in filehandle:
    lineno = lineno + 1
    if(lineno > 4):
        print line

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.