Finding errors in a file
I have a huge file whose contents are generated from running an executable over and over on different input files. The file's pattern is such: -file name followed by an arbitrary amount of text lines. I have to pick up the n开发者_运维技巧ame of the file when there is an error in reading input data and I am not sure what the best way to do it is. Another problem is that the word error comes up every time anyway in a phrase (Final fitting error was (some numerical value)) which needs to be ignored.
C:\temptest\blahblah1
.. (arbitrary # of text lines)
Final fitting error : (some number) [I have to ignore this]
C:\temptest\blahblah2
.. (arbitrary # of text lines)
Error could not read data !** [I have to pick up blahblah2 and copy the file to another directory, but just logging the name would suffice]
Thanks in advance !
This should do more or less what you need:
f = open("your_file.txt")
file_name = None
for line in f:
if line.startswith(r"C:\"):
file_name = line
elif line.startswith("Error"):
print "Error for file " + file_name
Assumptions:
- File names will start with "C:\", if that isn't true use a regular expression to perform a more accurate match or insert a special character before new files as you mentioned in a comment.
- There will only be one error per file, or printing multiple errors for a file is not a problem. If that is not the case, set some flag when you first print an error for a file and skip all subsequent errors until you find a new file.
So your log file looks like
{filepath}\file1
{
multiple lines
}
Final fitting error : 3.2
{filepath}\file2
{
multiple lines
}
Error could not read data !
and you want a list of all filenames resulting in the 'Error could not read data' message?
import re
import os.path
skipErrs = set("Final fitting error")
saveErrs = set("Error could not read data")
LOOKFOR = re.compile('(' + '|'.join(skipErrs) + '|' + '|'.join(saveErrs) + ')')
class EOF_Exception(Exception): pass
def getLine(f):
t = f.readline()
if t=='':
raise EOF_Exception('found end of file')
else:
return t.strip()
def getFilePath(f):
return os.path.normpath(getLine(f))
errorfiles = []
with open('logfile.txt') as inf:
while True:
try:
filepath = getFilePath(inf)
s = getLine(f)
m = re.match(s)
while not m:
s = getLine(f)
m = re.match(s)
if m.group(1) in saveErrs:
errorfiles.append(filepath)
except EOF_Exception:
break
With special being whatever header you want to append to the file lines:
[line[len(special):].strip() for line in file if line.startswith(special)]
You could use regexes also, but it will be more robust to add your own header, unless you are sure arbitrary lines could not start with a valid file name.
import shutil
f=open("file")
o=open("log","a")
for line in f:
if line.lstrip().startswith("C:"):
filename = line
if "Error" in line or "error" in line:
o.write( filename +"\n")
shutil.move(line,another_directory)
f.close()
o.close()
精彩评论