title: Python - Local variable referenced before assignment error - reading a CSV file
body: <p>I have the following Python 2.7 code. The aim is to read the csv file and run through the script for each row in the CSV file.</p>
  
  <p>The CSV file looks like this (no headers, all filenames in column A - one file name per row, not comma separated) </p>
  
  <pre><code>ftp_abc_REFRESH_a
  ftp_dep_a
  </code></pre>
  
  <p>The script works for the first file, but fails on the second file with the following error:</p>
  
  <pre><code>timestamp  = oupload.stat(file).st_atime
  UnboundLocalError: local variable 'file' referenced before assignment
  </code></pre>
  
  <p>Any help is appreciated. Thanks :) </p>
  
  <p>The script:</p>
  
  <pre><code>def myscript(ofile):  
  
      import time
      import pysftp 
      import sys
      import os
      from datetime import datetime
      import calendar
      import zipfile
      import re
  
      oupload = pysftp.Connection(host="hostbcd", username="admin", password="abcs")
  
      d = datetime.utcnow()
      unixtime=calendar.timegm(d.utctimetuple())
  
      month = datetime.now().strftime("%m")
  
  
      string = ofile+month+".*\.txt$"
  
      possibleFiles = oupload.listdir("/")
      for filename in possibleFiles:
              filedate = re.search(string, filename)
              if filedate:
                  file = filename
  
      timestamp  = oupload.stat(file).st_atime  
      #if timestamp &gt; unixtime - 54050 - 30 days
      if timestamp != unixtime:
          newtime=unixtime + 1800
          abmfile='filename_'
          zipname = abmfile+str(newtime)+'.sync.zip'
          create_zip = zipfile.ZipFile(zipname, 'w', zipfile.ZIP_DEFLATED)
          oupload.get(file,file)
          oupload.close()
          newfilename = abmfile+str(newtime)+'.sync' 
          os.rename(file, newfilename)
          create_zip.write(newfilename)
          create_zip.close()
  
      else: 
          print "No files found"
  
  import csv
  with open("data.csv", 'r') as csv_file:
      reader = csv.reader(csv_file)
  
      for row in reader:
          ofile = row[0]
          result = myscript(ofile)
  </code></pre>