-1

I am trying to run a python script in a number of files, and read the output for. Currently it only checks if the file exists by:

if file == "output":

But this output dosn't confirm if the run is finished properly. When the run is finished properly, the last-but-one line of the file is: " This run finished at "

How can I read this last-but-one line file? With my current knowledge in python (novice), I have managed to do:

#!/usr/bin/python3
import os
input = "FeMnPSi_EOS_10."
for subdirs, dirs, files in os.walk(input):
    for file in files:
        if file == "out-Dy-eos2":
            print("file found")
            ifile=input+"/"+file
            with open(ifile, "r") as if1:
                for line in if1:
                    if "This run finished at" in line:
                        print("Ends Properly")

The problem with this code is:

  • It does not know, if the "This run finished at" line is the penultimate line of the file.
  • I am not sure, but guess, here python has to read the file sequentially....for this 10000+ lines for each file.

So, can I make it better in checking if the line do exist, and among last two line, without reading the whole file?

BaRud
  • 3,055
  • 7
  • 41
  • 89
  • 1
    here's [how to read the last several lines from a file](http://stackoverflow.com/a/260433/4279). Though 10K+ is small therefore you could just `if1.readlines()[-2:]` to get the last 2 lines. – jfs Sep 25 '14 at 11:17
  • @J.F.Sebastian OP didn't say how long his lines were, and he's asking for an efficient method. – simonzack Sep 25 '14 at 11:18
  • @simonzack: And? have you noticed the link in my previous comment? – jfs Sep 25 '14 at 11:21
  • @J.F.Sebastian I like the algorithm you linked (better than the answers I linked) but just don't think your `readlines` response is very relevant. – simonzack Sep 25 '14 at 11:23
  • @simonzack: [premature optimization is the root of all evil](http://c2.com/cgi/wiki?PrematureOptimization). Unless OP have measured it and proved that the simple solution is too slow; you should not try more complex solutions. – jfs Sep 25 '14 at 11:54
  • use `ifile = os.path.join(subdirs, file)` instead of `ifile = input+"/"+file` – jfs Sep 25 '14 at 11:56
  • Or if OP is designing his application to process large files. – simonzack Sep 25 '14 at 12:23
  • @simonzack: 10K+ is small as I've already said. – jfs Sep 27 '14 at 13:36
  • @J.F.Sebastian Not if you're doing it over a few hundred files :) – Veedrac Sep 28 '14 at 05:35
  • @Veedrac: are you sure? Have you measured it? I'm not against more complex solutions once you've proven that you need it. That is why I've provided the link to the solution that handles large files well first, and only then (only then) suggested the simpler less efficient solution. My point is that the simple solution is relevant; it doesn't mean that you should always use it. – jfs Sep 28 '14 at 15:14
  • @J.F.Sebastian I agree. – Veedrac Sep 28 '14 at 15:16

1 Answers1

-1

The bit you're missing is if1.readlines() which reads the whole file in as a list of strings. You can then access the last-but-one element using slice notation.

with open(ifile, 'rb') as if1:
    if "This run finished at" in if1.readlines()[-2]
        print("Ends Properly")
Jamie Bull
  • 12,889
  • 15
  • 77
  • 116