如何查看文件的更改? [英] How do I watch a file for changes?

查看:54
本文介绍了如何查看文件的更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个日志文件正在由另一个进程写入,我想查看更改.每次发生更改时,我都想读取新数据以对其进行一些处理.

I have a log file being written by another process which I want to watch for changes. Each time a change occurs I'd like to read the new data in to do some processing on it.

这样做的最佳方法是什么?我希望 PyWin32 库中会有某种钩子.我找到了 win32file.FindNextChangeNotification 函数,但不知道如何让它观看特定文件.

What's the best way to do this? I was hoping there'd be some sort of hook from the PyWin32 library. I've found the win32file.FindNextChangeNotification function but have no idea how to ask it to watch a specific file.

如果有人做过这样的事情,我会很感激听到如何...

If anyone's done anything like this I'd be really grateful to hear how...

我应该提到我追求的是不需要轮询的解决方案.

I should have mentioned that I was after a solution that doesn't require polling.

诅咒!这似乎不适用于映射的网络驱动器.我猜 Windows 不会像在本地磁盘上那样听到"文件的任何更新.

Curses! It seems this doesn't work over a mapped network drive. I'm guessing windows doesn't 'hear' any updates to the file the way it does on a local disk.

推荐答案

您是否已经看过 http://timgolden.me.uk/python/win32_how_do_i/watch_directory_for_changes.html?如果您只需要它在 Windows 下工作,那么第二个示例似乎正是您想要的(如果您将目录路径与您要观看的文件之一交换).

Have you already looked at the documentation available on http://timgolden.me.uk/python/win32_how_do_i/watch_directory_for_changes.html? If you only need it to work under Windows the 2nd example seems to be exactly what you want (if you exchange the path of the directory with one of the files you want to watch).

否则,轮询可能是唯一真正独立于平台的选项.

Otherwise, polling will probably be the only really platform-independent option.

注意:这些解决方案我都没有尝试过.

Note: I haven't tried any of these solutions.

这篇关于如何查看文件的更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆