{"id":220,"date":"2016-08-02T14:33:12","date_gmt":"2016-08-02T18:33:12","guid":{"rendered":"http:\/\/williamkennerly.com\/blog\/?p=220"},"modified":"2016-08-05T07:30:09","modified_gmt":"2016-08-05T11:30:09","slug":"python-error-handling-temporary-files-and-skipping-failed-jobs","status":"publish","type":"post","link":"http:\/\/williamkennerly.com\/blog\/python-error-handling-temporary-files-and-skipping-failed-jobs\/","title":{"rendered":"Python Error Handling: Temporary Files and Skipping Failed Jobs"},"content":{"rendered":"<p>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:<\/p>\n<p><code>import glob<\/p>\n<p>#get a folder to search through<br \/>\nfolder_location = get_folder_location()<\/p>\n<p>#gets filepaths for everything in the specified folder ending with .LOG or .out<br \/>\nsearch_str_log = folder_location + \"*.LOG\"<br \/>\nsearch_str_out = folder_location + \"*.out\"<\/p>\n<p>file_paths_log = glob.glob(search_str_log)<br \/>\nfile_paths_out = glob.glob(search_str_out)<\/p>\n<p>file_paths = file_paths_log + file_paths_out<\/code><\/p>\n<p>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 <a href=\"https:\/\/community.intuit.com\/articles\/1021370-display-hidden-files-and-folders-or-search-for-files-using-windows\">this link<\/a> for instructions on how to do that). Then I deleted the temporary files, so they would not be read as extra files.<\/p>\n<p>&nbsp;<\/p>\n<p>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 &#8220;Error termination&#8221;. Since I was reading output files with <code>.readlines()<\/code>, as a list of lines (strings), I decided to recombine the list of strings into one string as follows:<\/p>\n<p><code>#reads the file as a list of its lines of text<br \/>\ncontent_lines = current_file.readlines()<\/p>\n<p>combined = '\\t'.join(content_lines)<\/p>\n<p>if \"Error termination\" in combined:<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;print file_name + \" skipped because of error termination<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;in Gaussian.\"<\/p>\n<p>else:<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;#analyze file<\/code><\/p>\n<p>Another option would have been to use <code>.read()<\/code> in addition to <code>.readlines()<\/code> for a different variable <code>content<\/code>, but that would set the cursor to the end of the file, and I would need to write <code>current_file.seek(0)<\/code> to reset the cursor to the beginning of the file. (See <a href=\"http:\/\/williamkennerly.com\/blog\/python-single-point-geometry-energy-script-update\/\">this post<\/a> for more information about this issue.) Using the <code>.join()<\/code> command was a simpler way of getting the text file as a string, without having to set the cursor back.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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() &hellip; <a href=\"http:\/\/williamkennerly.com\/blog\/python-error-handling-temporary-files-and-skipping-failed-jobs\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[13,14],"class_list":["post-220","post","type-post","status-publish","format-standard","hentry","category-programming","tag-gaussian","tag-python"],"_links":{"self":[{"href":"http:\/\/williamkennerly.com\/blog\/wp-json\/wp\/v2\/posts\/220","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/williamkennerly.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/williamkennerly.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/williamkennerly.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/williamkennerly.com\/blog\/wp-json\/wp\/v2\/comments?post=220"}],"version-history":[{"count":10,"href":"http:\/\/williamkennerly.com\/blog\/wp-json\/wp\/v2\/posts\/220\/revisions"}],"predecessor-version":[{"id":239,"href":"http:\/\/williamkennerly.com\/blog\/wp-json\/wp\/v2\/posts\/220\/revisions\/239"}],"wp:attachment":[{"href":"http:\/\/williamkennerly.com\/blog\/wp-json\/wp\/v2\/media?parent=220"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/williamkennerly.com\/blog\/wp-json\/wp\/v2\/categories?post=220"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/williamkennerly.com\/blog\/wp-json\/wp\/v2\/tags?post=220"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}