dissidia
dissidia

Reputation: 1581

Reading from a certain line onwards in python

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

Upvotes: 0

Views: 811

Answers (3)

Archit Verma
Archit Verma

Reputation: 1917

This should work:

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

for line in lines:
    print line
    #do something

Upvotes: 1

grc
grc

Reputation: 23545

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

Upvotes: 2

Abbath
Abbath

Reputation: 592

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

Upvotes: 1

Related Questions