# 读取student_grade_input.txt文本文件内容
datas = read_file()
print("read_file datas:",datas)
# 对读取出来的内容可以进行排序
datas = sort_grades(datas)
print("sort_grades datas:",datas)
# #对排序整理过的文件我们进行输出到新的文本文档保存
write_file(datas)
#关于python中读取文件出现乱码,并且报错。
#在文本中,我们要保证统一的编码,比如都用utf-8  或者gb2312.
#统计学生高分,低分,平均分。
# 输入文件:
# 三列 学号 姓名 成绩
def compute_score():
    scores = []
    with open("./student_grade_input.txt",encoding="utf-8") as fin:
        for line in fin:
            line = line[:-1]
            fields = line.split(",")
            scores.append(int(fields[-1]))
    max_score = max(scores)
    min_score = min(scores)
    avg_score = round(sum(scores)/len(scores),2)
    return max_score,min_score,avg_score
max_score, min_score, avg_score = compute_score()
print(f"max_score = {max_score}, min_score = {min_score}, avg_score = {avg_score}")
#                                     (实用技能)   统计英语文章每个单词出现的次数
word_count = {}
with open("./student_grade_output.txt") as fin:
    for line in fin:
        line = line[:-1]
        words = line.split()
        for word in words:
            if word not in word_count:
                word_count[word] = 0
            word_count[word] += 1
print(
    sorted(
        word_count.items(),
        key=lambda x: x[1],
        reverse=True
    )[:10]
)
#                                  (实用技能)统计目录下所有文件的大小:
import os
print(os.path.getsize("student_grade_output.txt"))
sum_size = 0
for file in os.listdir("."):
    if os.path.isfile(file):
        sum_size += os.path.getsize(file)
print("all size of dir: ",sum_size/1000)