-1

I have my code named read in C:\Users\Win-8.1\Desktop\omi. Inside this folder, I have another folder named no2 containing 14 .nc files and nothing else. I am running my code on Spyder as follows:

import os
import glob
import netCDF4
path = 'C:\Users\Win-8.1\Desktop\omi\no2'
for filename in glob.glob(os.path.join(path,'*.nc')):
    data=netCDF4.Dataset('filename','r')
    print data

and this is the error I am getting:

runfile('C:/Users/Win-8.1/Desktop/omi/test.py', wdir='C:/Users/Win-8.1/Desktop/omi')
Reloaded modules: netCDF4
nekomatic
  • 5,988
  • 1
  • 20
  • 27
Ajgar Dev
  • 35
  • 8
  • 1
    No, it is not. And I can run the program fora single file, so I want to know what is the mistake I am making in the looping part of the code. – Ajgar Dev Jul 19 '18 at 03:25
  • 1
    That should be `Dataset(filename,'r')`, not `Dataset('filename','r')`. About the "`Reloaded modules: netCDF4`", never seen that before, might be a Spyder thing. Is that the full traceback/error? – Bart Jul 19 '18 at 04:28
  • 1
    Even now, it is giving the same error. – Ajgar Dev Jul 19 '18 at 05:36
  • 1
    Might be related to this: https://stackoverflow.com/questions/46485628/avoid-reloaded-modules-module-name-message-in-python ? I tried your code on my system, and there it runs without any problems (if I change `'filename'` to `filename`). – Bart Jul 19 '18 at 05:50
  • 1
    Now, I am getting this: runfile('C:/Users/Win-8.1/Desktop/omi/nc.py', wdir='C:/Users/Win-8.1/Desktop/omi'). It seems to be running, but it is not giving an output, which it did for a single file. Could you tell me how many test files did you run it for and how much computation time did it take? – Ajgar Dev Jul 19 '18 at 08:39
  • 1
    This shouldn't take any significant time for 14 NetCDF files, since they are only opened without reading the data. – Bart Jul 19 '18 at 08:42
  • 2
    Possible duplicate of [Windows path in Python](https://stackoverflow.com/questions/2953834/windows-path-in-python) – tripleee Jul 19 '18 at 09:05
  • 2
    The glob returns zero files because the directory doesn't exist. Use a raw string for `path` or replace the backslashes with forward slashes (or get rid of Windows; my pleasure, you're welcome). This is a very common FAQ. – tripleee Jul 19 '18 at 09:08
  • 1
    You have hit the nail right on the head. – Ajgar Dev Jul 19 '18 at 10:12
  • 1
    Then please accept the duplicate nomination. Thanks. – tripleee Jul 21 '18 at 03:11
  • 1
    How do you do that? – Ajgar Dev Jul 21 '18 at 10:27

1 Answers1

2

Thanks to tripleee, my code has started working. I am posting the working code as follows:

import os
import glob
import netCDF4
path = 'C:/Users/Win-8.1/Desktop/omi/no2'
    for filename in glob.glob(os.path.join(path,'*.nc')):
        data=netCDF4.Dataset(filename,'r')
        print data
Ajgar Dev
  • 35
  • 8