Not sure if I'm missing something obvious, but here's what is happening: I have a python 2.4.3 script that contains several RegEx objects. Below one of the regex objects is searching for all matches in a string (tMatchList). Even if tMatchList is not null, it is printing an empty set after the 'if p:' step. This behavior occurs even if it prints correctly before the 'if p:' step. I thought it may have been a scope issue, but everything is declared & contained within one function. I'm not quite seeing how the 'if p:' step is not able to see tMatchList. I am able to print tMatchList after the if statement as well.
tMatchList = []
for lines in r:
linecount += 1
tMatchList = self._testReplacePDFTag.findall(lines)
p = self._pdfPathRegex.search(lines)
print tMatchList #tMatchList is printing just fine here if it has any elements
if p:
print tMatchList #now it's empty,
#even if it printed elements in prior statement
lines = .....
else:
<something else gets done>
print tMatchList #now it prints again
Including entire function definition for those who would like to see it....
def FindFilesAndModifyPDFTag(self, inRootDirArg, inRollBackBool):
for root, dirs, files in os.walk(inRootDirArg):
for d in dirs:
if d.startswith('.'):#excludes directories that start with '.'
continue
for file in files:
if os.path.splitext(file)[1] == self._fileExt:
#Backup original. just do it
shutil.copy2(os.path.join(root, file), os.path.join(root, file)+"~")
r = open(os.path.join(root, file)+"~", "r")
f = open(os.path.join(root, file), "w")
linecount = 0
tMatchList = []
for lines in r:
linecount += 1
tMatchList = self._testReplacePDFTag.findall(lines)
t = self._testReplacePDFTag.search(lines)
#find pdf path(s) in line
pMatchList = self._pdfPathRegex.findall(lines)
p = self._pdfPathRegex.search(lines)
#fix the pdf tracking code
print id(tMatchList), "BEFORE"
if p:
print id(tMatchList), "INSIDE"
lines = self.processPDFTagLine(pMatchList, lines, linecount, file, tMatchList)
else:
lines = self.processCheckMetaTag(lines, linecount, file)
#print id(tMatchList), "INSIDE ELSE"
print id(tMatchList), "AFTER"
f.writelines(lines)
f.close()
r.close()
os.remove(os.path.join(root, file)+"~")
enter code here