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?