I have file /tmp/gs.pid with content
client01: 25778
I would like retrieve the second word from it.
ie. 25778.
I have tried below code but it didn’t work.
>>> f=open ("/tmp/gs.pid","r")
>>> for line in f:
... word=line.strip().lower()
... print "\n -->" , word
Try this:
It will print the second word of every line in lowercase.
split()will take your line and split it on any whitespace and return a list, then indexing with[1]will take the second element of the list andlower()will convert the result to lowercase. Note that it would make sense to check whether there are at least 2 words on the line, for example: