python glob issues with directory with [] in name

glob, python

Solution

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)

Problem

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 ```

Original source