0

I'm using python, and want to read a file that is being written with new data. However everytime I read the last line (waiting for the new line to be written in), it goes wrong. The new line isn't the same length or format as the old ones.. Is there a way to wait until a new \n appears, then read the line.

here is my code:


def get_mkt_data(mkt_data_fp):
    fields = mkt_data_fp.readline()
    fields = fields.split(",") # here is the columns
    while True:
        where = mkt_data_fp.tell()
        line = mkt_data_dp.readline().split(",")
        if len(line) == len(fields):
            row = dict(zip(fields, line))
            yield row
        else:
            mkt_data_dp.seek(where)
            time.sleep(1)






Bubblethan
  • 379
  • 3
  • 11
  • Does this answer your question? [How to read a file as it is being written in real time with Python 3 and asyncio, like "tail -f"](https://stackoverflow.com/questions/54492569/how-to-read-a-file-as-it-is-being-written-in-real-time-with-python-3-and-asyncio) – Multihunter Dec 11 '20 at 05:40
  • You could alternatively do file.readLines() or file.read().split("\n") to get a list of lines. – Timothy Chen Dec 11 '20 at 05:42
  • @TimothyChen both of those would be unidiomatic. `readlines` is a fossil kept around for no good reason, really. You should use `list(file)` – juanpa.arrivillaga Dec 11 '20 at 05:52

0 Answers0