为什么递归搜索文件夹中的文件不能正常工作

2024-04-25 07:07:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要帮助来理解为什么这段代码不能像预期的那样工作。你知道吗

我的目录结构如下所示:

|- tryWalkDir.py

   TryCPP/
   TryCPP/tryHashMap/
   TryCPP/tryHashMap/tryHashMap.cpp
   TryCPP/tryHashMap/tryHashMap.o*

剧本-tryWalkDir.py公司旨在搜索所有.cpp文件。我不知道为什么

[TryCPP/tryHashMap]/tryHashMap.cpp文件,TryCPP/tryHashMap/tryHashMap.cpp文件]收集2次?你知道吗

Enter into depth:0, folder:TryCPP
folder:TryCPP, cur:TryCPP, sub:['tryHashMap'], files:[], depth:0
recresively call - s:tryHashMap
Enter into depth:1, folder:TryCPP/tryHashMap
folder:TryCPP/tryHashMap, cur:TryCPP/tryHashMap, sub:[], files:
['tryHashMap.cpp', 'tryHashMap.o'], depth:1
process tryHashMap.cpp
append tryHashMap.cpp
process tryHashMap.o
Exit on depth:1, folder:TryCPP/tryHashMap
folder:TryCPP, cur:TryCPP/tryHashMap, sub:[], files:['tryHashMap.cpp', 'tryHashMap.o'], depth:0
process tryHashMap.cpp
append tryHashMap.cpp
process tryHashMap.o
Exit on depth:0, folder:TryCPP
['TryCPP/tryHashMap/tryHashMap.cpp', 'TryCPP/tryHashMap/tryHashMap.cpp']

你知道吗tryWalkDir.py公司你知道吗

class Cell(object):
    def __init__(self, fn, ext):    
       self.fn = fn
       self.ext = ext
       self.fl = [] #list all the files

    def collect_files(self, folder, depth=0):
    ''' collect all the folders containing corresponding extension scripts '''
        print 'Enter into depth:%d, folder:%s' % (depth,folder)

        # level one folder name should start with 'Try' or 'try'
        if depth == 1:
            filename = os.path.basename(folder)[:3]
            if filename in ['Try','try']:
                pass
            else:
                print 'L1 Dir - {0} must start with [Try,try], depth:{1}'.format(filename,depth)
            return

        for cur, sub, files in os.walk(folder):
            print 'folder:{}, cur:{}, sub:{}, files:{}, depth:{}'.format(folder,cur,sub,files,depth)

            #filter out all the files
            #[ self.fl.append(cur+'/'+f) for f in files if os.path.splitext(f)[1][1:] == self.ext ]
            for f in files:
                print 'process %s' % f
                if os.path.splitext(f)[1][1:] == self.ext:
                    print 'append %s' % f
                    self.fl.append(cur+'/'+f)

            #if sub:
            for s in sub:
                print 'recresively call - s:{}'.format(s)
                self.collect_files(cur+'/'+s,depth+1)

        print 'Exit on depth:%d, folder:%s' % (depth,folder)


    def start(self):
        self.collect_files(self.fn,0)
        #print self.fl


def main():
    cell = Cell('TryCPP','cpp')
    cell.start()
    print cell.fl

if __name__ == '__main__': main()

Tags: inselfiffilesfolderprocesscppext
1条回答
网友
1楼 · 发布于 2024-04-25 07:07:52

发生此错误是因为您多次调用^{}而没有意识到。os.walk为您递归到子目录中。但是,然后为当前目录中的每个子目录调用self.collect_files(cur+'/'+s,depth+1)。这实际上会导致一个深度为N的文件在输出数组中出现N次。你知道吗

要修复代码,只需删除循环即可

for s in sub:
    print 'recresively call - s:{}'.format(s)
    self.collect_files(cur+'/'+s,depth+1)

另外,您可能应该使用^{},而不是在整个代码中手动连接斜杠。例如,self.fl.append(cur+'/'+f)可以读取self.fl.append(join(cur, f))。这是os.walk文档建议的方法:

To get a full path (which begins with top) to a file or directory in dirpath, do os.path.join(dirpath, name).

相关问题 更多 >