I'm trying to get a value from a variable inside some files, here is the code:
path = '/opt/log/...'
word = 'somevariable'
def enumeratepaths(path=path):
paths = []
for dirpath, dirnames, files in os.walk(path):
for file in files:
fullpath = os.path.join(dirpath, file)
paths.append(fullpath)
return paths
def read_files(file):
try:
file_open = open(file, 'r')
search = file_open.read()
find = search.find(word)
find_start = find + 7
find_stop = find + 9
result = int(search[find_start:find_stop])
return int(result)
finally:
file_open.close()
def main():
for files in enumeratepaths():
read_files(files)
if __name__ == "__main__":
main()
The problem is that some files don't have the variable inside, because some error occurred. IN this case, this script returns an error:
result = int(search[find_start:find_stop])
ValueError: invalid literal for int(): RE
I want to get these values as int, but I'm stuck.
And another question: If the file does not have that search value, how can it be that it returns things like "RE"?