Initial commit
This commit is contained in:
27
Solutions/8_1/follow.py
Normal file
27
Solutions/8_1/follow.py
Normal file
@@ -0,0 +1,27 @@
|
||||
# follow.py
|
||||
import os
|
||||
import time
|
||||
|
||||
def follow(filename):
|
||||
'''
|
||||
Generator that produces a sequence of lines being written at the end of a file.
|
||||
'''
|
||||
with open(filename,'r') as f:
|
||||
f.seek(0,os.SEEK_END)
|
||||
while True:
|
||||
line = f.readline()
|
||||
if line == '':
|
||||
time.sleep(0.1) # Sleep briefly to avoid busy wait
|
||||
continue
|
||||
yield line
|
||||
|
||||
# Example use
|
||||
if __name__ == '__main__':
|
||||
for line in follow('../../Data/stocklog.csv'):
|
||||
fields = line.split(',')
|
||||
name = fields[0].strip('"')
|
||||
price = float(fields[1])
|
||||
change = float(fields[4])
|
||||
if change < 0:
|
||||
print('%10s %10.2f %10.2f' % (name, price, change))
|
||||
|
||||
Reference in New Issue
Block a user