How do I perform error handling with two files?
<p>So , I am having two files , so to checks its validity  I am performing <code>try</code> and <code>except</code> two times . But I don't thinks this is a good method, can you suggest a better way?</p>

<p>Here is my code:</p>

<pre><code>def form_density_dictionary(self,word_file,fp_exclude):
  self.freq_dictionary={}
  try:
      with open(fp_exclude,'r')as fp2:
          words_excluded=fp2.read().split() #words to be excluded stored in a list
          print("**Read file successfully :" + fp_exclude + "**")
          words_excluded=[words.lower() for words in words_excluded] # converted to lowercase

  except IOError:
      print("**Could not read file:", fp_exclude, " :Please check file name**")
      sys.exit()

  try:
      with open(word_file,'r') as file:
          print("**Read file successfully :" + word_file + "**")
          words_list=file.read()
          if not words_list:
              print("**No data in file:",word_file +":**")
              sys.exit()
          words_list=words_list.split()
          words_list=[words.lower() for words in words_list] # lowercasing entire list
      unique_words=list((set(words_list)-set(words_excluded)))

      self.freq_dictionary= {word:("%6.2f"%(float((words_list.count(word))/len(words_list))*100))  for word in unique_words}
      #print((len(self.freq_dictionary)))
  except IOError:
      print("**Could not read file:", word_file, " :Please check file name**")
      sys.exit()
</code></pre>

<p>Any other suggestion is also welcomed to make it more pythonic.</p>

<p>To be more 'pythonic' you could use something what is callec Counter, from collections library.</p>

<pre><code>from collections import Counter


def form_density_dictionary(self, word_file, fp_exclude):
    success_msg = '*Read file succesfully : {filename}'
    fail_msg = '**Could not read file: {filename}: Please check filename'
    empty_file_msg = '*No data in file :{filename}:**'
    exclude_read = self._file_open(fp_exclude, success_msg, fail_msg, '') 
    exclude = Counter([word.lower() for word in exclude_read.split()])
    word_file_read = self._file_open(word_file, success_msg, fail_msg, empty_file_msg)
    words = Counter([word.lower() for word in word_file_read.split()])
    unique_words = words - excluded
    self.freq_dictionary = {word: '{.2f}'.format(count / len(unique_words)) 
                            for word, count in unique_words.items()}
</code></pre>

<p>Also it would be better if you would just create the open_file method, like:</p>

<pre><code>def _open_file(self, filename, success_msg, fails_msg, empty_file_msg):
    try:
       with open(filename, 'r') as file:
           if success_msg:
               print(success_msg.format(filename= filename))
           data = file.read()
           if empty_file_msg:
               print(empty_file_msg.format(filename= filename))
           return data
    except IOError:
           if fail_msg:
               print(fail_msg.format(filename= filename))
           sys.exit()
</code></pre>

python python-3.x error-handling