【发布时间】:2010-02-26 19:17:45
【问题描述】:
我正在尝试在 Python 中使用正则表达式将文件(保存为字符串,即“/volumes/footage/foo/bar.mov”)与我创建的包含文件列表的日志文件匹配。但是当我运行脚本时,它给了我这个错误:sre_constants.error: unbalanced parenthesis。我使用的代码是这样的:
读取文件:
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()
然后在for 循环中我重新分配(直到我发布这个问题我才意识到我正在这样做)the_file 变量作为文件列表中的字符串(通过运行文件夹获得,它是子集并获取所有文件名),然后尝试使用正则表达式查看该文件名是否存在于日志文件中:
for the_file in filenamelist:
p = re.compile(the_file, re.IGNORECASE)
m = p.search(the_log)
每次遇到代码的re.compile() 部分时,它都会吐出该错误。如果我尝试删除它,并使用re.search(the_file, the_log),它仍然会吐出那个错误。我不明白我怎么会从中得到不平衡的括号。
【问题讨论】: