async watch directory for new files

Zoran zljubisic at gmail.com
Sun Apr 25 14:50:43 EDT 2021


> <https://duckduckgo.com/?q=python+file+system+watcher> 
> 
> Implementations are usually just callback-based. (Apologies for the 
> generic link, I haven't needed this in Python yet: anyway, those are the 
> keywords.) 

:) before asking a question here I googled the subject a lot.
Anyway, the simplest solution is this one:

import asyncio
import pathlib
from watchgod import awatch, Change

watch_dir = pathlib.Path(r'C:\Users\User\watch_folder')


async def main():
    async for changes in awatch(str(watch_dir)):
        while changes:
            change_type, path = changes.pop()
            if change_type == Change.added:
                print('processing:', path)
            

loop = asyncio.get_event_loop()
loop.run_until_complete(main())

watchgod library is quite young. I hope that there will be no suprises.


More information about the Python-list mailing list