如何查看文件中的更改?

    技术2022-07-11  74

    本文翻译自:How do I watch a file for changes?

    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. 每次发生更改时,我都希望读入新数据以对其进行一些处理。

    What's the best way to do this? 最好的方法是什么? I was hoping there'd be some sort of hook from the PyWin32 library. 我希望从PyWin32库中获得某种吸引。 I've found the win32file.FindNextChangeNotification function but have no idea how to ask it to watch a specific file. 我找到了win32file.FindNextChangeNotification函数,但不知道如何要求它监视特定文件。

    If anyone's done anything like this I'd be really grateful to hear how... 如果有人做了这样的事情,我将不胜感激。

    [Edit] I should have mentioned that I was after a solution that doesn't require polling. [编辑]我应该提到我所追求的是不需要轮询的解决方案。

    [Edit] 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. 我猜想Windows不会像在本地磁盘上那样“听到”文件的任何更新。


    #1楼

    参考:https://stackoom.com/question/lOf/如何查看文件中的更改


    #2楼

    This is another modification of Tim Goldan's script that runs on unix types and adds a simple watcher for file modification by using a dict (file=>time). 这是Tim Goldan脚本的另一种修改,该脚本可在Unix类型上运行,并通过使用dict(file => time)添加了一个简单的文件修改监视程序。

    usage: whateverName.py path_to_dir_to_watch 用法:whateverName.py path_to_dir_to_watch

    #!/usr/bin/env python import os, sys, time def files_to_timestamp(path): files = [os.path.join(path, f) for f in os.listdir(path)] return dict ([(f, os.path.getmtime(f)) for f in files]) if __name__ == "__main__": path_to_watch = sys.argv[1] print('Watching {}..'.format(path_to_watch)) before = files_to_timestamp(path_to_watch) while 1: time.sleep (2) after = files_to_timestamp(path_to_watch) added = [f for f in after.keys() if not f in before.keys()] removed = [f for f in before.keys() if not f in after.keys()] modified = [] for f in before.keys(): if not f in removed: if os.path.getmtime(f) != before.get(f): modified.append(f) if added: print('Added: {}'.format(', '.join(added))) if removed: print('Removed: {}'.format(', '.join(removed))) if modified: print('Modified: {}'.format(', '.join(modified))) before = after

    #3楼

    I don't know any Windows specific function. 我不知道Windows的任何特定功能。 You could try getting the MD5 hash of the file every second/minute/hour (depends on how fast you need it) and compare it to the last hash. 您可以尝试每秒钟/分钟/小时获取文件的MD5哈希值(取决于您需要的速度),然后将其与最后一个哈希值进行比较。 When it differs you know the file has been changed and you read out the newest lines. 如果不同,您将知道文件已更改,并读出了最新的行。


    #4楼

    I'd try something like this. 我会尝试这样的事情。

    try: f = open(filePath) except IOError: print "No such file: %s" % filePath raw_input("Press Enter to close window") try: lines = f.readlines() while True: line = f.readline() try: if not line: time.sleep(1) else: functionThatAnalisesTheLine(line) except Exception, e: # handle the exception somehow (for example, log the trace) and raise the same exception again raw_input("Press Enter to close window") raise e finally: f.close()

    The loop checks if there is a new line(s) since last time file was read - if there is, it's read and passed to the functionThatAnalisesTheLine function. 循环检查自从上次读取文件以来是否存在新行-如果存在,则读取该行并将其传递给functionThatAnalisesTheLine函数。 If not, script waits 1 second and retries the process. 如果不是,脚本将等待1秒钟,然后重试该过程。


    #5楼

    Check my answer to a similar question . 检查我对类似问题的回答 。 You could try the same loop in Python. 您可以在Python中尝试相同的循环。 This page suggests: 该页面建议:

    import time while 1: where = file.tell() line = file.readline() if not line: time.sleep(1) file.seek(where) else: print line, # already has newline

    Also see the question tail() a file with Python . 另请参见问题tail()使用Python的文件 。


    #6楼

    Have you already looked at the documentation available on http://timgolden.me.uk/python/win32_how_do_i/watch_directory_for_changes.html ? 您是否已经看过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 the one of the file you want to watch). 如果只需要它在Windows下运行,则第二个示例似乎正是您想要的(如果您将目录的路径与要观看的文件之一交换)。

    Otherwise, polling will probably be the only really platform-independent option. 否则,轮询将可能是唯一真正与平台无关的选项。

    Note: I haven't tried any of these solutions. 注意:我还没有尝试过这些解决方案。

    Processed: 0.010, SQL: 9