한때 파이썬으로 작성된 파서에 대해 일종의 데몬 프로세스를 만들었습니다. 일련의 파일을보고 Python으로 처리해야했지만, 진정한 멀티 OS 솔루션 (이 경우 Windows & 리눅스)이어야했습니다. 필자는 수정 시간을 확인하여 파일 목록을 감시하는 프로그램을 작성했습니다. 이 프로그램은 잠시 잠자기 후 감시되는 파일의 수정 시간을 확인합니다. 수정 시간이 이전에 등록 된 것보다 새로운 경우 파일이 변경되었으므로이 파일로 작업해야합니다. 이 같은
뭔가 :
import os
import time
path = os.path.dirname(__file__)
print "Looking for files in", path, "..."
# get interesting files
files = [{"file" : f} for f in os.listdir(path) if os.path.isfile(f) and os.path.splitext(f)[1].lower() == ".src"]
for f in files:
f["output"] = os.path.splitext(f["file"])[0] + ".out"
f["modtime"] = os.path.getmtime(f["file"]) - 10
print " watching:", f["file"]
while True:
# sleep for a while
time.sleep(0.5)
# check if anything changed
for f in files:
# is mod time of file is newer than the one registered?
if os.path.getmtime(f["file"]) > f["modtime"]:
# store new time and...
f["modtime"] = os.path.getmtime(f["file"])
print f["file"], "has changed..."
# do your stuff here
그것은 최고 수준의 코드처럼 보이지 않는,하지만 그것은 아주 잘 작동합니다.
이 이것에 대해 다른 SO 질문은,하지만 귀하의 질문에 직접 답을 제공 있을지 모르겠어요 :
How to implement a pythonic equivalent of tail -F?
How do I watch a file for changes?
How can I "watch" a file for modification/change?
희망 이게 도움이됩니다!
'tail -f'를 사용할 수 있습니까? –
정확히 바이너리 파일로 재생되지 않습니다 :) – fbd39j