Python Error Handling: Temporary Files and Skipping Failed Jobs

In my scripts that analyze all Gaussian output files in a directory, I use glob to get all the files into a list. The relevant script is as follows:

import glob

#get a folder to search through
folder_location = get_folder_location()

#gets filepaths for everything in the specified folder ending with .LOG or .out
search_str_log = folder_location + "*.LOG"
search_str_out = folder_location + "*.out"

file_paths_log = glob.glob(search_str_log)
file_paths_out = glob.glob(search_str_out)

file_paths = file_paths_log + file_paths_out

I have been editing and testing output files within the directory, and my script has mistakenly read their temporary Word files as .out files. I needed to make those temporary files visible (see this link for instructions on how to do that). Then I deleted the temporary files, so they would not be read as extra files.

 

I wanted my script to skip files that had in error termination in Gaussian, so I needed to search the output files for the string “Error termination”. Since I was reading output files with .readlines(), as a list of lines (strings), I decided to recombine the list of strings into one string as follows:

#reads the file as a list of its lines of text
content_lines = current_file.readlines()

combined = '\t'.join(content_lines)

if "Error termination" in combined:
    print file_name + " skipped because of error termination
    in Gaussian."

else:
    #analyze file

Another option would have been to use .read() in addition to .readlines() for a different variable content, but that would set the cursor to the end of the file, and I would need to write current_file.seek(0) to reset the cursor to the beginning of the file. (See this post for more information about this issue.) Using the .join() command was a simpler way of getting the text file as a string, without having to set the cursor back.

2 Comments

Filed under programming

2 Responses to Python Error Handling: Temporary Files and Skipping Failed Jobs

  1. It would be more clear to give an example of those “ghost” temporary filenames that Word makes when you open output file using Word. An even better idea would be just not to use Word 🙂 I believe the files started with “~” so you could even have your scripts just skip over any such files so you wouldn’t need to delete them.

    Its interesting that you’re literally joining the WHOLE output file into one string. How large are the output files you’ve tested it on? If its a huge file, like 100,000 lines, does it get slow? I’ve never tried such a thing. What you did works and is easy to code. If the code ever gets slow you could probably have the file iterator seek to the 10th from last line of file and then look for “Error Termination”

    Lastly remember not all failed Gaussian jobs say “Error Termination”. A good mini-project for someone could be to write a python function to catch all the possible failed Gaussian jobs.

  2. Pingback: Python Error-Handling: Follow-up to Temporary Files | Computational Chemistry at Skidmore College

Leave a Reply