Put your try/except structure in other blocks. Otherwise, when you receive an error message, it will break all cycles.
Perhaps after the first for loop add try/except . Then, if an error occurs, it will continue with the subsequent file.
for infile in listing: try: if infile.startswith("ABC"): fo = open(infile,"r") for line in fo: if line.startswith("REVIEW"): print infile fo.close() except: pass
This is a great example of why you should use the with statement here to open files. When you open a file with open() , but an error is thrown, the file remains open forever. Now better than never.
for infile in listing: try: if infile.startswith("ABC"): with open(infile,"r") as fo for line in fo: if line.startswith("REVIEW"): print infile except: pass
Now, if the error is caught, the file will be closed, as the with statement does.
Terrya
source share