-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathpipe_recent_gen.py
46 lines (34 loc) · 935 Bytes
/
pipe_recent_gen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# Code Listing #12
"""
Using generators, print details of the most recently modified file, matching a pattern.
"""
# pipe_recent_gen.py
import glob
import os
from time import sleep
def watch(pattern):
""" Watch a folder for modified files matching a pattern """
while True:
files = glob.glob(pattern)
# sort by modified time
files = sorted(files, key=os.path.getmtime)
recent = files[-1]
yield recent
# Sleep a bit
sleep(1)
def get(input):
""" For a given file input, print its meta data """
for item in input:
data = os.popen("ls -lh " + item).read()
# Clear screen
os.system("clear")
yield data
if __name__ == "__main__":
import sys
# Source
stream1 = watch('*.' + sys.argv[1])
while True:
# Filter
stream2 = get(stream1)
print(stream2.__next__())
sleep(2)