开发者

Another Python Scope Question - losing information going into if statement

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


The findall may not create a list object. If it is some kind of generator function, then it has a value which is "consumed" by traversing the results once.

After consuming the results yielded by this function, there are no more results.

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, 

Try this.

tMatchList = list( self._testReplacePDFTag.findall(lines) )


So this is what ended up 'fixing' the issue: I moved the tMatchList findall() line to follow the search. Then I added an 'if t:' statement. Now I am seeing the content of tMatchList inside the 'if p:' statement when I print it out. So problem solved for now, but I'm thinking there is some sort of behavioral concern I'm not aware of concerning the re module(?) or evaluating an empty list object.

#original code ....
linecount += 1

#this is modified section
t = self._testReplacePDFTag.search(lines)
if t:
    tMatchList = self._testReplacePDFTag.findall(lines)

#end modified section

pMatchList = self._pdfPathRegex.findall(lines)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜