试图遍历目录和子目录我在以下代码中遇到错误。在这里,我试图递归调用构造函数。
import os
from os.path import isfile, join
class CovPopulate:
fileList = list()
def __init__(self,path):
self.path = path
for f in os.listdir(self.path):
if isfile(join(self.path,f)):
if f.endswith(".txt"):
fileList.append(join(self.path,f))
else:
CovPopulate(f)
追溯 : -
CovPopulate(r"C:\temp")
File "<pyshell#1>", line 1, in <module>
CovPopulate(r"C:\temp")
File "C:/fuzzingresults/CovPopulate.py", line 11, in __init__
fileList.append(join(self.path,f))
NameError: global name 'fileList' is not defined
但是,我已经定义了 fileList = list()
这次我检查了同步错误:/