use fileinput to read a specific line

Martin Marcher martin at marcher.name
Tue Jan 8 05:04:33 EST 2008


jo3c wrote:

> i need to read line 4 from a header file

http://docs.python.org/lib/module-linecache.html

~/2delete $ cat data.txt
L1
L2
L3
L4

~/2delete $ python
Python 2.5.1 (r251:54863, May  2 2007, 16:56:35)
[GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import linecache
>>> linecache.getline("data.txt", 2)
'L2\n'
>>> linecache.getline("data.txt", 5)
''
>>> linecache.getline("data.txt", 1)
'L1\n'
>>>


-- 
http://noneisyours.marcher.name
http://feeds.feedburner.com/NoneIsYours

You are not free to read this message,
by doing so, you have violated my licence
and are required to urinate publicly. Thank you.




More information about the Python-list mailing list