0

I'm trying to read the output file of a simulation and grap some data (elapsed CPU time). I've tried multiple ways of doing this. But I can't get past one row in the file, that seems to work like an escape character.

This is the last row (row 92 of 3846) Python detects in the file:

expanding memory to 6775753 d 3496497

After this the function stops reading the file.

It's not a big file (189KB).

Is there any way of skipping this row? A solution that starts reading the file from the back would be sufficient as well, since the data I'm trying to access is towards the end of the file.

This is the code I'm trying to use. I tried to read the file in binary, but it didn't solve my problem.

txt_file = os.path.join(dir_register, 'messag')
with open(txt_file, 'r+b') as txt:
    for line in txt.readlines():
        if 'Total CPU time'.encode() in line:
            cpu_time = float(line.split()[4])
martineau
  • 119,623
  • 25
  • 170
  • 301
  • Is there anything unusual about line 92? Note you do not need to read the entire file into memory — which is what `readlines()` does — because you can loop over the file object line-by-line with `for line in txt:`. You will probably need to make the file available for download somewhere for anyone to help you. – martineau Jun 24 '22 at 16:41
  • You can read a file line-by-line in binary mode via: `for line in iter(txt.readline, b""):`. You can also try reading it in reverse order using one of the techniques shown in [this answer](https://stackoverflow.com/a/54932624/355230) of mine. – martineau Jun 24 '22 at 16:51
  • Can you share a part of the file (at least until line 94)? – nonDucor Jun 24 '22 at 16:57
  • wht is the line before and the line after please ? it looks like the code might break here: `if 'Total CPU time'.encode() in line:` the commuinity can answer this for you. – D.L Jun 24 '22 at 20:34

0 Answers0