Skip to content
Advertisement

My code is confusing an input file name for a regex expression

My regular expression does not explicitly include a dash in a character range, but my code fails when the input file name is like this:

JavaScript

Here is my code:

JavaScript

It seems obvious that this part of the filename is the issue: [Maxi-Single]

How do I handle filenames similar to that so that they are treated as fixed strings, not part of the regex expression?

(Not my main question, but in case it is related, I am open to try an alternative method of making a case-insensitive search. I have looked at several Stack Overflow questions on that topic and I didn’t — so far — find any solutions that seemed to fit this case.)

Here is my error:

Traceback (most recent call last):

JavaScript

EDIT: I tried using re.escape which is referenced here: https://docs.python.org/3/library/re.html

JavaScript

It processed the earlier filename but now fails with the input filename Aerosmith - Aerosmith (2014) [24-96 HD] producing the same error at the same point in my revised code.

Advertisement

Answer

Rather than using glob with funny file patterns passed through root, you are better off sorting out just the names, and then prepend the root. One possible one-liner:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement