查看 python 中的目录并解析最新生成的文本文件
Watch a directory in python and parse latest generated text file
我想问一下我们是否可以通过任何方式查看 python 中的目录并解析目录中生成的最新文本文件。
虽然我有这个解析特定文本文件的启动代码。
import time
def follow(thefile):
thefile.seek(0,2)
while True:
line = thefile.readline()
if not line:
time.sleep(0.1)
continue
yield line
if __name__ == '__main__':
logfile = open(r'\some directory\files.txt',"r")
loglines = follow(logfile)
for line in loglines:
print line,
看到粗体 files.txt 我需要它是动态的,方法是查看新生成的文本文件的目录并切换到最新的文本文件并解析它。
它将 运行 在 Windows XP 服务包 3
上
我正在使用 Python 2.7
我正在观看的目录也在使用 windows XP
谢谢。
看看 FindFirstChangeNotification
API
http://timgolden.me.uk/python/win32_how_do_i/watch_directory_for_changes.html
The approach here is to use the MS FindFirstChangeNotification API, exposed via the pywin32 win32file module. It needs a little explanation: you get a change handle for a directory (optionally with its subdirectories) for certain kinds of change. You then use the ubiquitous WaitForSingleObject call from win32event, which fires when something's changed in one of your directories.
本质上是因为 Windows OS 负责管理 creation/modification 个文件,您可以要求它在文件 changed/created 时立即通知您。
要检查新文件,请重复获取 os.listdir('directory')
目录中当前的文件列表。将条目保存在一个集合中,并计算该集合与上一个集合的差异。
# Initialize before an event loop:
old_entries = set()
# You need a loop that calls two handlers, each handler returning soon.
# Inside your loop, check for a "new file" event this way:
now_entries = os.listdir(r'\some directory')
now_entries.symmetric_difference_update(old_entries)
for new_entry in now_entries:
handle_new_file(new_entry)
您的程序需要监听两个事件:
- 目录中有新文件。
- 旧文件中的新行。
您调用 follow()
,这就像一个永远不会 return 的事件处理程序。我认为您希望该处理程序 return 到一个检查每种事件的主事件循环。您的 follow()
函数永远不会 return,因为它 continue
在 while True
无限循环中,除非将新行添加到文件以使其成为 yield
。如果没有更多的行被添加到该文件,它永远不会屈服。
我想问一下我们是否可以通过任何方式查看 python 中的目录并解析目录中生成的最新文本文件。
虽然我有这个解析特定文本文件的启动代码。
import time
def follow(thefile):
thefile.seek(0,2)
while True:
line = thefile.readline()
if not line:
time.sleep(0.1)
continue
yield line
if __name__ == '__main__':
logfile = open(r'\some directory\files.txt',"r")
loglines = follow(logfile)
for line in loglines:
print line,
看到粗体 files.txt 我需要它是动态的,方法是查看新生成的文本文件的目录并切换到最新的文本文件并解析它。
它将 运行 在 Windows XP 服务包 3
上我正在使用 Python 2.7
我正在观看的目录也在使用 windows XP
谢谢。
看看 FindFirstChangeNotification
API
http://timgolden.me.uk/python/win32_how_do_i/watch_directory_for_changes.html
The approach here is to use the MS FindFirstChangeNotification API, exposed via the pywin32 win32file module. It needs a little explanation: you get a change handle for a directory (optionally with its subdirectories) for certain kinds of change. You then use the ubiquitous WaitForSingleObject call from win32event, which fires when something's changed in one of your directories.
本质上是因为 Windows OS 负责管理 creation/modification 个文件,您可以要求它在文件 changed/created 时立即通知您。
要检查新文件,请重复获取 os.listdir('directory')
目录中当前的文件列表。将条目保存在一个集合中,并计算该集合与上一个集合的差异。
# Initialize before an event loop:
old_entries = set()
# You need a loop that calls two handlers, each handler returning soon.
# Inside your loop, check for a "new file" event this way:
now_entries = os.listdir(r'\some directory')
now_entries.symmetric_difference_update(old_entries)
for new_entry in now_entries:
handle_new_file(new_entry)
您的程序需要监听两个事件:
- 目录中有新文件。
- 旧文件中的新行。
您调用 follow()
,这就像一个永远不会 return 的事件处理程序。我认为您希望该处理程序 return 到一个检查每种事件的主事件循环。您的 follow()
函数永远不会 return,因为它 continue
在 while True
无限循环中,除非将新行添加到文件以使其成为 yield
。如果没有更多的行被添加到该文件,它永远不会屈服。