file - Reading specific lines only (Python) -


i'm using loop read file, want read specific lines, line #26 , #30. there built-in feature achieve this?

thanks

if file read big, , don't want read whole file in memory @ once:

fp = open("file") i, line in enumerate(fp):     if == 25:         # 26th line     elif == 29:         # 30th line     elif > 29:         break fp.close() 

note i == n-1 nth line.


in python 2.6 or later:

with open("file") fp:     i, line in enumerate(fp):         if == 25:             # 26th line         elif == 29:             # 30th line         elif > 29:             break 

Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -