I’m trying to use a regex in Python to match a file (saved as a string, ie “/volumes/footage/foo/bar.mov”) to a log file I create that contains a list of files. But when I run the script, it gives me this error: sre_constants.error: unbalanced parenthesis. The code I’m using is this:
To read the file:
theLogFile = The_Root_Path + ".processedlog"
if os.path.isfile(theLogFile):
the_file = open(theLogFile, "r")
else:
open(theLogFile, 'w').close()
the_file = open(theLogFile, "r")
the_log = the_file.read()
the_file.close()
Then inside a for loop I reassign (I didn’t realize I was doing this until I posted this question) the the_file variable as a string from a list of files (obtained by running through a folder and it’s subsets and grabbing all the filenames), then try to use regex to see if that filename is present in the log file:
for the_file in filenamelist:
p = re.compile(the_file, re.IGNORECASE)
m = p.search(the_log)
Every time it hits the re.compile() part of the code it spits out that error. And if I try to cut that out, and use re.search(the_file, the_log) it still spits out that error. I don’t understand how I could be getting unbalanced parenthesis from this.
Gordon,
it would seem to me that the issue is in the data. You are compiling uninspected strings from the
filelistinto regexp, not heeding that they might contain meta characters relevant for the regexp engine.In your for loop, add a
print the_filebefore the call to re.compile (it is no problem that you are re-using a name as the loop iterator that referred to file object before), so you can see which strings are actually coming from the filelist. Or, better still, run all instances of the_file throughre.escapebefore passing them to re.compile. This will turn all meta characters into their normal equivalent.