pythonloggingpython-loggingpython-os

How to easily log all disk io in Python


As the title says, Is there an easy way to log all disk operations using the os library such as os.remove(...) and the standard open(...)? That includes automatic flushes to files, so just aliasing or overriding the functions used in the code is not enough.


Solution

  • One of the ways these could be archived is via Python Audit hooks described in https://peps.python.org/pep-0578/ and the table of actual hooks here https://docs.python.org/3/library/audit_events.html#audit-events

    As op mentioned, there is no hook to monitor flushes and general writes. However, maybe as extra layer, one could monitor open() calls and attach something like https://pythonhosted.org/watchdog/ to explicitly monitor those