def get_files(file_path):
file_list = []
#遍历文件夹
for root, dirs, files in os.walk(file_path):
if not files:
continue
for file in files:
compelete_path = os.path.join(root, file)
file_list.append(complete_path)
return file_list
def get_number(file_list, number_compile):
number_list = []
f = open(file_list, 'r')
contents = f.read()
f.close()
numbers = number_compile.findall(contents)
for number in numbers:
number_list.append(number)
sum = 0
c = 0
for i in range(len(number_list)):
sum = float(number_list[i])+sum
c += 1
if c == 0:
pass
else:
avg_number = sum/c
return avg_number
if __name__ == "__main__":
file_path = "./test"
number_compile = re.compile(r"\d+")
file_names = get_files(file_path)
for file_name in file_names:
result = get_number(file_name, number_compile)
print file_name, result