-
Notifications
You must be signed in to change notification settings - Fork 0
/
watch_folder.py
executable file
·46 lines (32 loc) · 1.03 KB
/
watch_folder.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
#!/usr/bin/env python3
import argparse
from pathlib import Path
import time
POLL_INTERVAL_SEC = 60
MIN_AGE_SEC = 60
def watch_folder(indir, outfile):
items = set()
if Path(outfile).exists():
items = set(line.strip() for line in open(outfile))
with open(outfile, 'a') as outf:
while True:
for p in Path(indir).glob('*.h5'):
abspath = str(p.absolute())
if abspath in items:
continue
if time.time() - p.stat().st_mtime < 60:
continue
outf.write(f'{abspath}\n')
items.add(abspath)
print(f'Added {abspath}')
outf.flush()
print('Sleeping...')
time.sleep(POLL_INTERVAL_SEC)
def main():
ap = argparse.ArgumentParser()
ap.add_argument('-i', '--indir', required=True)
ap.add_argument('-o', '--outfile', required=True)
args = ap.parse_args()
watch_folder(args.indir, args.outfile)
if __name__ == '__main__':
main()