如何查看文件中的更改?

本文翻譯自: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. 注意:我還沒有嘗試過這些解決方案。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章