I am trying to open a log file that remains open by another process and deletes the first few lines. On Unix, I would just do os.open('/tmp/file.log', os.O_NONBLOCK) , and that would bring me closer to my goal.
Now I'm stuck on Windows, and I need to somehow rotate this log without finishing the application containing the file. Is it possible?
At first I considered opening a file descriptor in the place where the application expected the log to be, and just acts as a pipe in the file descriptor in Python, but I could not find a way to do this on Windows.
I also thought that I just move the file on a regular basis and let the application recreate the file, but since it is used by another process that does not bring much benefit.
Thought O_SHLOCK , but again, that is Unix, not Windows. So I went for the mmap file and I hope that this makes it a little more flexible, but it does not lead to anything.
import mmap import contextlib import time with open(r'test.log', 'r+') as f: with contextlib.closing(mmap.mmap(f.fileno(), 0)) as m: while 1: line = m.readline() if len(line) > 0: print line time.sleep(0.5)
This results in the application being unable to access the file because Python is holding it (and vice versa).
I had to think about signal.SIGHUP , but it does not exist on Windows, so back to the square.
I am stuck and I tried everything, can Python help me here or do I need to switch the language?