r/Python • u/Eskwire • Oct 27 '14
Getting this error when run py script.
SO theres a .py http://pastebin.com/K0c4chnQ and lately getthing this error:
focker/Anime/Hajime no Ippo/Season 1/[Utsukushii-Raws] Hajime no Ippo - The Fighting - 07 (DVD 720x480 H264 AC3 2.0 Chap Sub).mkv Looking for /Users/hectorhenry/Downloads/focker/Anime/Hajime no Ippo/Season 1/[Utsukushii-Raws] Hajime no Ippo - The Fighting - 07 (DVD 720x480 H264 AC3 2.0 Chap Sub)* Traceback (most recent call last): File "AutoDelete.py", line 387, in <module> changed += CheckShows(file); File "AutoDelete.py", line 158, in CheckShows for deleteFile in glob.glob(similairFiles): File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/glob.py", line 27, in glob return list(iglob(pathname)) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/glob.py", line 59, in iglob for name in glob_in_dir(dirname, basename): File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/glob.py", line 78, in glob1 return fnmatch.filter(names, pattern) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/fnmatch.py", line 54, in filter _cache[pat] = re.compile(res) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/re.py", line 190, in compile return _compile(pattern, flags) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/re.py", line 242, in _compile raise error, v # invalid expression sre_constants.error: bad character range
any idea what the problem and how can i solve it. i dont have idea of python but will have to learn. thaks
2
u/yaxriifgyn Oct 27 '14
oh my. clean up the formatting. please.
You have a problem in the regular expression you are passing to glob.
The reason is the file name, which you printed in "Looking for ...", contains characters which are non-literal in regular expressions, i.e. they are special characters in regular expressions. You need to escape these characters so the re compiler does not tread them as special.
1
1
u/fancy_pantser Nov 09 '14
Glob doesn't take regular expressions. It has only three kinds of simple matching operators. To escape characters, you enclose them individually in [square brackets].
glob.escape()will handle it for you.
7
u/fancy_pantser Oct 27 '14
This script sucks
The variables names are misspelled (i.e. "similair")
A glob.glob() call to the regex module is failing because it's not sanitizing the filenames. Wrap it in a try/catch and print a warning and then continue on if that's what you want.