Skip to content

Instantly share code, notes, and snippets.

@sebasmonia
Created December 9, 2016 19:02
Show Gist options
  • Save sebasmonia/581b0635843c3c4aaeb0a9f4bb6efaed to your computer and use it in GitHub Desktop.
Save sebasmonia/581b0635843c3c4aaeb0a9f4bb6efaed to your computer and use it in GitHub Desktop.
I don't know if this makes any sense
import os
import asyncio
async def watch (dir_to_watch, callbacks, interval=2, fire_for_existing_files=True):
counter = 0
current_contents = None
callbacks = callbacks
if not fire_for_existing_files:
before = set(os.listdir(dir_to_watch))
else:
before = set()
while True:
await asyncio.sleep(interval)
after = set(os.listdir(dir_to_watch))
added = after-before
removed = before-after
unchanged = before.intersection(after)
if added or removed:
changes = (added, removed, unchanged)
for func in callbacks:
func(changes)
before = after
return
def print_changes(to_print):
new, deleted, same = to_print
print("new files:", new)
print("deleted files:", deleted)
print("files unchanged:", same)
#to call:
#>>>loop = asyncio.get_event_loop()
#>>> loop.run_until_complete(dirwatcher.watch("C:/test", [dirwatcher.print_changes], interval=5))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment