4

I am using glob to find all *.shp files within a directory, but the directory name contains '[]' and that is causing glob to fail. Any workarounds for this?

My code is:

glob.glob(sub_dir+os.sep+'soilmu_a_*.shp')

where sub_dir is:

'C:\\Users\\oh\\wss_SSA_OH001_soildb_OH_2003_[2013-12-19]\\spatial\\'

The error message I get is:

*** error: bad character range
user308827
  • 21,227
  • 87
  • 254
  • 417
  • note: you can use `/` as path separator. Python will translate it for you as appropriate for your platform (easier to read paths that way). – isedev Feb 26 '14 at 23:39

4 Answers4

3

As suggested in the manual page, you can modify your pattern and wrap the offending meta characters. Change [ to [[] and ] to []] (single character ranges corresponding to the meta character).

For instance:

pattern = sub_dir + os.se p +'soilmu_a_*.shp'
pattern = pattern.replace('[','[[]').replace(']','[]]')
glob.glob(pattern)
isedev
  • 18,848
  • 3
  • 60
  • 59
  • 2
    Thanks, but this does not return a file match even though one does exists at that path. Could it be that by doing the pattern replacement, we construct a path that no longer exists? – user308827 Feb 26 '14 at 23:31
  • well `[[]` is simply a range expression containing only `[` so matches only the `[` character. same applies to `[]]` with the `]` character... so no. – isedev Feb 26 '14 at 23:37
  • one thing to note: your `sub_dir` already contains a trailing path separator and you're appending another. perhaps that's the problem... – isedev Feb 26 '14 at 23:38
  • 2
    Your double .replace won't work. Here is what will work: .translate({ord('['):'[[]', ord(']'):'[]]'}) – Megan Caithlyn Sep 27 '17 at 17:34
  • 1
    double translation is likely to fail. – shouldsee May 16 '19 at 19:24
  • @EliaIliashenko Why would the double replace fail? I tried it out on an ordinary string on the python terminal and it works absolutely fine. – Mugen Feb 13 '20 at 03:41
  • 2
    @Mugen because the second replace will replace „]“ symbols which appeared as a result of 1st replace. – Megan Caithlyn Feb 14 '20 at 07:41
  • 1
    @EliaIliashenko Beautiful! Thanks! – Mugen Feb 16 '20 at 15:33
1

@isedev is right about the character range but double replacement is NOT a sensible thing to do.

import re
ptn = re.sub('([\[\]])','[\\1]',ptn)

str.translate() should also work

shouldsee
  • 434
  • 7
  • 7
0

Yes, glob failed to find subdirectories when the parent directory path had [] in it, and I got NO error message! I found the problem by chance. I could wish that I had gotten an error message.

I switched to os.listdir to get the subdirectories, and since I needed the full path to the subdirectories, I had to paste that back in:

subdirs = [d for d in os.listdir(current_full_path) if os.path.isdir(current_full_path + '/' + d)]
for subdir in subdirs:
    subdir_full_path = current_full_path + '/' + subdir
excyberlabber
  • 629
  • 1
  • 10
  • 17
0

From Python 3.4 version you can use glob.escape(path) that automatically escapes all special characters ('?', '*' and '[') in literal string so in your case:

glob.glob(glob.escape(sub_dir) + os.sep + 'soilmu_a_*.shp')

or

sub_dir = glob.escape(subdir)
glob.glob(sub_dir + os.sep + 'soilmu_a_*.shp')
DannyG
  • 141
  • 1
  • 5