seek in a file

Neal Norwitz neal at metaslash.com
Tue Mar 26 18:31:48 EST 2002


Andreas Penzel wrote:
> 
> Hello NG!
> 
> How to jump to a specified line in a file which is read-opened?
> With seek() I can jump to an exact position of  the complete file in
> "byte-steps".
> Is seek able to jump to a line-number?
> If not, what else can I do?

seek() only works in bytes.  This should work:

>>> f = open('/etc/termcap')
>>> lines = f.readlines()
>>> print lines[0]
######## TERMINAL TYPE DESCRIPTIONS SOURCE FILE

>>> print lines[1] 
#

etc.  lines is a list of strings (which include the newlines).

Neal



More information about the Python-list mailing list