Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As vy32 noted, writing to the same file you're currently reading can be tricky (with the exact behaviour being OS dependent).</p> <p>It also isn't entirely clear if the file you're reading is being updated live by another process. I'm going to assume that is the case, as simple line iteration would otherwise do the trick.</p> <p>When reading a live file, the standard iterator isn't really what you want, as it will terminate as soon as it encounters an EOF marker. To monitor a live file, you either need to periodically poll it for new data, or else set up one of the OS-specific notification mechanisms that lets you know when new data is available.</p> <p>There are some good answers to that question over here: <a href="https://stackoverflow.com/questions/182197/how-do-i-watch-a-file-for-changes-using-python">How do I watch a file for changes?</a></p> <p>Writing back to the same file is still going to be a potential problem though, especially if another process has it open for writing (even if you get the concurrent access to work, it will be tricky to get the monitoring program to ignore its own writes without risking missing writes from the application being monitored).</p> <p>Example of using the file line iterator with collections.deque to remember context information while still benefiting from all the I/O optimisations built into the line iterator:</p> <pre><code>context = deque(maxlen=10) # Remember most recent 10 lines for line in f: # Process the current line context.append(line) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload