统计缺陷数量,会自动遍历文件夹下所有的txt格式文件,并将每一类缺陷数量累加求和。
import os
def count_first_column_chars(folder_path):
char_count = {}
for root, dirs, files in os.walk(folder_path):
for file in files:
if file.endswith('.txt'):
file_path = os.path.join(root, file)
with open(file_path, 'r', encoding='utf-8') as f:
for line in f:
if line.strip(): # Skip empty lines
first_char = line.split()[0] # Assume columns are separated by whitespace
char_count[first_char] = char_count.get(first_char, 0) + len(first_char)
return char_count
if __name__ == "__main__":
folder_path = input("输入目录")
result = count_first_column_chars(folder_path)
print("每一行第一列的字符数量统计:")
for char, count in result.items():
print(f"字符 '{char}' 总数为: {count}")