Intercept (using Python) data being written to a file from another process

孤街醉人 提交于 2019-12-13 18:09:11

问题


I am working on something where this could come in handy in the future.

Does anyone know of a way I can intercept data (using Python) being written to a file (via some other language/process)?

I would know the path of the file I want to intercept and I preferably want to find a solution that would work on Windows. I know watchdog can watch for file changes but my goal would be to intercept the write before it touches the file.

For example they I have the following script running on my computer that just constantly writes to a file:

import time
filename = "testfile"
i = 1
while True:
    with open(filename, 'a') as out:
        out.write(str(i) + '\n')
        time.sleep(1)
        i += 1

Note: This is just an example. The data I want to intercept is not being written with Python. I don't know what it is written with.

In another script, I want to intercept everything being written to testfile.

I don't believe this is possible but I figured I would ask.


回答1:


Using os.walk you can make a list of how many files you have in your whole directory and then keep checking it and cross reference it with a previous variable that says what the file count is, and when there is a difference you can open it using os.open.



来源:https://stackoverflow.com/questions/55716275/intercept-using-python-data-being-written-to-a-file-from-another-process

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!