Python os.walk netCDF4 not working together
<p>I am trying to go through a series of .nc files to run a small bit of code. The test script below prints the first filename in the directory but when I use <code>ncfile = netCDF4.Dataset(fname, 'r')</code>  I get the error </p>

<pre><code>File "netCDF4\_netCDF4.pyx", 
line 1795, in netCDF4._netCDF4.Dataset.__init__ 
(netCDF4\_netCDF4.c:12278)
    RuntimeError: No such file or directory
</code></pre>

<p>Is this an incompatibility issue with os.walk and netCDF4 or is it a simple error I am making?</p>

<pre><code>import os
import netCDF4

for root, dirs, files in os.walk('E:\satellite .nc data\ENVISAT2006'):
   for fname in files:
       print fname # works up to here and without line below it prints all filenames 
       ncfile = netCDF4.Dataset(fname, 'r')
</code></pre>

<p>It seems it was a simple problem of needing the path and directory in</p>

<p><code>ncfile = netCDF4.Dataset(fname, 'r')</code> </p>

<p>so I have replaced it with</p>

<p><code>ncfile = netCDF4.Dataset(os.path.join(fdir,fname), 'r')</code> </p>

<p>and specified <code>fdir</code> outside the loop. For simplicity I have replaced <code>os.walk</code> with <code>os.listdir</code> as I don't need to go through a directory tree.</p>

<pre><code>import os
import netCDF4
import numpy as np
from math import pi
from numpy import cos, sin

fdir = 'E:\satellite .nc data\ENVISAT2006'
for fname in os.listdir('E:\satellite .nc data\ENVISAT2006'):
#os.walk only needed if going through all of the files in a directory tree
   #for fname in files:
    print fname 
    ncfile = netCDF4.Dataset(os.path.join(fdir,fname), 'r')
</code></pre>

python netcdf os.walk