I stumbled on glob.glob (), so make a note.
With glob.glob (), you cannot get the path containing [].
Example
glob.glob(r"d:\test\[1].*")
#=> [1].Does not match txt
glob.glob(r"d:\test\\[1\].*") # \[ \]You can't write
[ -> [[]
] -> []]
glob.glob(r"d:\test\[[]1[]].*")
#=> [1].Matches txt
You can also use the following function instead of glob.glob ().
def escapeBraceForGlob(str):
'''
convert [ -> [[] , ] -> []]
'''
newStr = str.replace("[","\\[").replace("]","\\]")
newStr = newStr.replace("\\[","[[]").replace("\\]","[]]")
return newStr
def globEscapeBraces(pathname):
'''
glob.glob() after escaping "[" and "]".
'''
return glob.glob(escapeBraceForGlob(pathname))
Recommended Posts