1

I am reading a folder of multiple images and here's the part of code where i read the folder specified in path,

path =  'C:\main\folder\sub-folder\08001\V.1\abc\2015'

for infile in glob.glob( os.path.join(path, '*.tif') ):

    Img = gdal.Open( infile )
    if Img is None:
      print 'Unable to open Input Image'
      sys.exit(1)

... so the code doesn't work as in it doesn't read any files in that folder but it also doesn't throw any error. I figured that it's not working only when the path has any of the Sub-folder's name starting with numeric values.

Here Path = 'C:\main\folder\sub-folder\08001\V.1\abc\2015' and as you can see it has two Sub-folder's (08001 and 2015) starting with numeric values.

** It works absolutely fine when there are no such sub-folder's starting with numeric values.

Why does this happen and how can i fix this ?? Also how can i automatically look into any sub-folder's if exist in the original path provided ?

when i look for that path in command shell, here's what it shows.

>>> path =  'C:\P\S\Mo\C\08008\L\V.1R\2015'
>>> path
'C:\\P\\S\\Mo\\C\x008008\\L\\V.1R\x815'
PythonLearner
  • 85
  • 1
  • 1
  • 5
  • See also [Python 2.6: “Couldn't open image” error](http://stackoverflow.com/questions/14718969/python-2-6-couldnt-open-image-error), which had the same underlying problem but with non-numeric paths. – Lithis Jul 28 '15 at 21:52

1 Answers1

0

You need to escape the backslashes in your string:

path =  'C:\\P\\S\\Mo\\C\\08008\\L\\V.1R\\2015'

It's just coincidence that your path works when there are no numbers in it. If you had a path with a folder that began with a, b, n, or certain other letters, it wouldn't work either.

A backslash followed by three numbers is parsed as an octal number whose value to insert into the string. The first such sequence is \080. 8 is not a valid octal digit, so Python stops parsing octal after the zero. This is displayed in hexadecimal as \x00 in your output. The second sequence is \201, and octal 201 is equal to hexadecimal 81—\x81.

Alternatively, you can use a raw string:

path =  r'C:\P\S\Mo\C\08008\L\V.1R\2015'

The r before the string literal tells Python not to parse escape sequences in the string. When working with Windows paths, this can make your paths look much nicer.

A third possibility is to use forward slashes instead of backslashes in your path string:

path =  'C:/P/S/Mo/C/08008/L/V.1R/2015'
Lithis
  • 1,327
  • 8
  • 14
  • That solves the problem. Thank You very much for the info, that gives a better understanding! – PythonLearner Jul 28 '15 at 21:59
  • Any idea on how can i also read the subfolders in the main path provided using the same gob.glob ? – PythonLearner Jul 28 '15 at 22:05
  • A Google search for "Python glob.glob recursive" found a previous SO question, [How can I search sub-folders using glob.glob module in Python?](http://stackoverflow.com/questions/14798220/how-can-i-search-sub-folders-using-glob-glob-module-in-python). – Lithis Jul 28 '15 at 23:53