import itertools
def readwords(file_object):
byte_stream = itertools.groupby(
itertools.takewhile(lambda c: bool(c),
itertools.imap(file_object.read,
itertools.repeat(1))), str.isspace)
return ("".join(group) for pred, group in byte_stream if not pred)
# Example usage
import sys
if __name__ == '__main__':
# read from a user file
with open(sys.argv[1], 'r') as f:
for w in readwords(f):
print (w)
# read from stdin
for w in readwords(sys.stdin):
print (w)