Returning first word on a line in a file by line number example
Published: Wednesday, Dec 26, 2007 Last modified: Thursday, Nov 14, 2024
Note raise! Very useful.
#!/usr/bin/env python2
def getlineword(filelist, lineno):
"""
Line number one is the first line
Returns first word on the line.
"""
try:
return filelist[lineno-1].split()[0]
except:
raise
if __name__ == '__main__':
"""
Does a head
"""
file = open('../file_list')
filelist = file.readlines()
for i in range(1,11):
print 'Line no: %s\tEntry: %s' % (i, getlineword(filelist, i))
file.close()