import os



# 找到指定后缀的文件
def find_type(path:str,fix:str):
    # path = os.getcwd()
    # print("path",path)
    list=os.listdir(path)
    # print("list",list)
    file_list=[]
    for i in list:
        ps=os.path.join(path, i)
        if os.path.isdir(ps):
            # print("path join",ps)
            file_list+=find_type(ps,fix)
        else:
            if(ps[-len(fix):]==fix):
                file_list.append(ps)
    # print("file_list", file_list)
    return file_list



def conv(file:str):
    s=""
    try:
        with open(file,encoding="gb2312") as f:
            s=f.read()
        os.remove(file)
        with open(file,mode="w+",encoding="utf-8") as f:
            f.write(s)
    except Exception as e:
        print("conv failed",file)

def run_conv(path:list):
    files=[]
    for i in path:
        files+=find_type(i,".c")
        files+=find_type(i,".h")
    print("find files:",len(files))
    for i in files:
        conv(i)





# 把此文件放到要转码的文件夹下，编码转化为utf-8
if __name__ == "__main__":
    run_conv(["./"])
