在第二个if中加了一行代码,将递归的返回值也添加到列表中,即将下一层文件夹中的文件总行数返回一并统计到该层文件夹的文件总行数中。我没有测试,你试一下是否正确。def search_file(start_dir):
os.chdir(start_dir)
print(os.getcwd())
target = ['.c', '.cpp', '.py', '.cc', '.java', '.pas', '.asm']
line_num = []
sum = 0
for each_file in os.listdir():
ext = os.path.splitext(each_file)[1]
if ext in target:
line_num.append(calc_code(each_file))
print(line_num)
if os.path.isdir(each_file):
lines=search_file(os.getcwd() + '/' +each_file)
line_num.append(lines)
os.chdir('..')
for j in range(len(line_num)):
sum += int(line_num[j])
return sum
|