title: Python Script - Skip file with error and continue analyzing other files
body: <p>I use the below code to extract data between 2 keywords from multiple txt files and store all data in an output text file. If a txt file has any issues, the code stops executing at that particular file and writes the result as &quot;Error in : &quot;. How do I modify this code so that, the code skips the file with error and continue analyzing other files in the directory</p>
  <pre><code>import os
  BASE_DIRECTORY = os.getcwd()
  oFullFileName =  BASE_DIRECTORY + &quot;\\&quot; +  &quot;1_Version_Info_Extract.txt&quot;
  
  file_list = []
  output = {}
  import csv
  
  outputfile = None
  lineCnt = 0
  def StartWrite():
      oFile = open(oFullFileName, 'a')
      return oFile
      
  def WriteIntoFile(f,line):
      outputfile.write(f + &quot;^&quot; + line)
     
  def StopWrite():
      outputfile.close()
      
  ## get list of .las files from current dir
  for (dirpath, dirnames, filenames) in os.walk(BASE_DIRECTORY):
      for f in filenames:
          if str(f).endswith(&quot;.txt&quot;) or str(f).endswith(&quot;.TXT&quot;):
              e = os.path.join(str(dirpath), str(f))
              file_list.append(e)
  
  ## create outfile
  outputfile = StartWrite()
  
  ## read file
  try:
  
      for f in file_list:
          txtfile = open(f, 'r')
          lineCnt = 0
  
          ## read file data 
          for line in txtfile:
              if (line[0:2] == &quot;~V&quot;) or (lineCnt &gt; 0):
                  ## write out req data into outfile 
                  WriteIntoFile(f,line)
  
                  ## Close reading current file
                  if (line[0:1] == &quot;~&quot; and (lineCnt &gt; 0)):
                      break
                  lineCnt = lineCnt + 1
  except :
      WriteIntoFile(f, 'Error in : ' + f)
  ## stop writing in outfile 
  StopWrite() 
  </code></pre>