title: How to fix UnboundLocalError: local variable 'df' referenced before assignment in Python
body: <p>I am communicating with <code>pysimplegui</code> but I am having the <code>UnboundLocalError</code> within one of my functions.</p>
  <p>Here is the <code>function</code>:</p>
  <pre><code>def load_file(file):
      file_details = file.split('.')
      
      if file_details[1] == &quot;csv&quot;:
          df = pd.read_csv(file)
          
      elif file_details[1] == &quot;xlsx&quot;:
          df = pd.read_excel(file)
          
      elif file_details[1] != &quot;csv&quot; and file_details[1] != &quot;xlsx&quot;:
          sg.popup(&quot;Unsupported file type&quot;)
      
      else:
          sg.popup(&quot;Your file does not meet the requirements&quot;, &quot;Please check the file requirements for more info&quot;)
          
      return df
  </code></pre>
  <p>What could be wrong with my code?</p>