title: Problems with variable referenced before assignment when using os.path.walk
body: <p>OK. I have some background in Matlab and I'm now switching to Python.
  I have this bit of code under Pythnon 2.6.5 on 64-bit Linux which scrolls through directories, finds files named 'GeneralData.dat', retrieves some data from them and stitches them into a new data set:</p>
  
  <pre><code>import pylab as p
  import os, re
  import linecache as ln
  
  def LoadGenomeMeanSize(arg, dirname, files):
          for file in files:
              filepath = os.path.join(dirname, file)
              if filepath == os.path.join(dirname,'GeneralData.dat'):
                  data = p.genfromtxt(filepath)
                  if data[-1,4] != 0.0: # checking if data set is OK 
                      data_chopped = data[1000:-1,:] # removing some of data
                      Grand_mean = data_chopped[:,2].mean()
                      Grand_STD = p.sqrt((sum(data_chopped[:,4]*data_chopped[:,3]**2) + sum((data_chopped[:,2]-Grand_mean)**2))/sum(data_chopped[:,4]))
                  else:
                      break
              if filepath == os.path.join(dirname,'ModelParams.dat'):
                  l = re.split(" ", ln.getline(filepath, 6))
                  turb_param = float(l[2])                
                  arg.append((Grand_mean, Grand_STD, turb_param))
  
  GrandMeansData = []
  os.path.walk(os.getcwd(), LoadGenomeMeanSize, GrandMeansData)
  GrandMeansData = sorted(GrandMeansData, key=lambda data_sort: data_sort[2])
  
  TheMeans = p.zeros((len(GrandMeansData), 3 ))
  i = 0
  for item in GrandMeansData:
      TheMeans[i,0] = item[0]
      TheMeans[i,1] = item[1]
      TheMeans[i,2] = item[2]
      i += 1
  
  print TheMeans # just checking...
  # later do some computation on TheMeans in NumPy
  </code></pre>
  
  <p>And it throws me this (though I would swear it was working a month ego):</p>
  
  <pre><code>Traceback (most recent call last):
    File "/home/User/01_PyScripts/TESTtest.py", line 29, in &lt;module&gt;
      os.path.walk(os.getcwd(), LoadGenomeMeanSize, GrandMeansData)
    File "/usr/lib/python2.6/posixpath.py", line 233, in walk
      walk(name, func, arg)
    File "/usr/lib/python2.6/posixpath.py", line 225, in walk
      func(arg, top, names)
    File "/home/User/01_PyScripts/TESTtest.py", line 26, in LoadGenomeMeanSize
      arg.append((Grand_mean, Grand_STD, turb_param))
  UnboundLocalError: local variable 'Grand_mean' referenced before assignment
  </code></pre>
  
  <p>All right... so I went and did some reading and came up with this global variable:</p>
  
  <pre><code>import pylab as p
  import os, re
  import linecache as ln
  
  Grand_mean = p.nan
  Grand_STD = p.nan
  def LoadGenomeMeanSize(arg, dirname, files):
          for file in files:
              global Grand_mean
              global Grand_STD
              filepath = os.path.join(dirname, file)
              if filepath == os.path.join(dirname,'GeneralData.dat'):
                  data = p.genfromtxt(filepath)
                  if data[-1,4] != 0.0: # checking if data set is OK 
                      data_chopped = data[1000:-1,:]  # removing some of data
                      Grand_mean = data_chopped[:,2].mean()
                      Grand_STD = p.sqrt((sum(data_chopped[:,4]*data_chopped[:,3]**2) + sum((data_chopped[:,2]-Grand_mean)**2))/sum(data_chopped[:,4]))
                  else:
                      break
              if filepath == os.path.join(dirname,'ModelParams.dat'):
                  l = re.split(" ", ln.getline(filepath, 6))
                  turb_param = float(l[2])                
                  arg.append((Grand_mean, Grand_STD, turb_param))
  
  GrandMeansData = []
  os.path.walk(os.getcwd(), LoadGenomeMeanSize, GrandMeansData)
  GrandMeansData = sorted(GrandMeansData, key=lambda data_sort: data_sort[2])
  
  TheMeans = p.zeros((len(GrandMeansData), 3 ))
  i = 0
  for item in GrandMeansData:
      TheMeans[i,0] = item[0]
      TheMeans[i,1] = item[1]
      TheMeans[i,2] = item[2]
      i += 1
  
  print TheMeans # just checking...
  # later do some computation on TheMeans in NumPy
  </code></pre>
  
  <p>It does not give error massages. Even gives a file with data... but data are bloody wrong! I checked some of them manually by running commands:</p>
  
  <pre><code>import pylab as p
  data = p.genfromtxt(filepath)
  data_chopped = data[1000:-1,:]
  Grand_mean = data_chopped[:,2].mean()
  Grand_STD = p.sqrt((sum(data_chopped[:,4]*data_chopped[:,3]**2) \
  + sum((data_chopped[:,2]-Grand_mean)**2))/sum(data_chopped[:,4]))
  </code></pre>
  
  <p>on selected files. They are different :-(</p>
  
  <p>1) Can anyone explain me what's wrong?</p>
  
  <p>2) Does anyone know a solution to that?</p>
  
  <p>I'll be grateful for help :-)</p>
  
  <p>Cheers,
       PTR</p>