Python code to ignore errors -


i have code stops running each time there error. there way add code script ignore errors , keep running script until completion?

below code:

import sys import tldextract  def main(argv):          in_file = argv[1]         f = open(in_file,'r')         urllist = f.readlines()         f.close()         destlist = []          in urllist:             print             str0 =             ch in ['\n','\r']:                     if ch in str0:                         str0 = str0.replace(ch,'')             str1 = str(tldextract.extract(str0))              str2 = i.replace('\n','') + str1.replace("extractresult",":")+'\n'             destlist.append(str2)          f = open('destfile.txt','w')         in destlist:                 f.write(i)          f.close()          print "completed successfully:"   if __name__== "__main__":     main(sys.argv) 

many thanks

you should 'try' open files. way can manage exceptions, if file not exist example. take loot @ python tutorial exeption handling

import sys  try:     f = open('myfile.txt')     s = f.readline()     = int(s.strip()) except ioerror e:     print "i/o error({0}): {1}".format(e.errno, e.strerror) except valueerror:     print "could not convert data integer." except:     print "unexpected error:", sys.exc_info()[0]     raise 

or

for arg in sys.argv[1:]:     try:         f = open(arg, 'r')     except ioerror:         print 'cannot open', arg     else:         print arg, 'has', len(f.readlines()), 'lines'         f.close() 

do not(!) 'pass' in exception block. will(!) make fall on face harder.


Comments

Popular posts from this blog

javascript - How to process users in one specific order using map o each function? -

java - Two interface have same method name with different return type -

javascript - Linking from page A to a specific iframe on page B -