Make python code continue after exception

后端 未结 3 1385
情深已故
情深已故 2021-02-01 02:53

I\'m trying to read all files from a folder that matches a certain criteria. My program crashes once I have an exception raised. I am trying to continue even if there\'s an exc

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-01 03:09

    Move the try/except inside the for loop. Like in:

      import os 
        path = 'C:\\'
        listing = os.listdir(path)
        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:
                  print "error "+str(IOError)
    

提交回复
热议问题