import os
import hashlib
import re
import time

cwd=os.getcwd()

if not re.match("ZJ-\d{3}",os.path.split(cwd)[-1]):
    print("请在选手文件夹: ZJ-### 下运行, 3秒后自动退出")
    time.sleep(3)
    exit()
else:
    for home,dirs,files in os.walk(cwd):
        #print(home,dirs,files)
        for filename in files:
            name,ext=os.path.splitext(filename)
            if os.path.split(home)[-1]==name and ext in ['.cpp','.c','.pas']:
                filepath=os.path.join(home,filename)
                print(filepath)
                with open(filepath,'r') as fp:
                    data=fp.read()
                file_md5=hashlib.md5(data.encode("utf-8")).hexdigest()
                print(file_md5)
                md5file=os.path.join(home,'md5')
                with open(md5file,'w') as md5_file:
                    md5_file.write(file_md5)
    input()
