import os import shutil def leetcode_helper(): data_dir = 'data/3.算法高阶/1.leetcode' dailycode_exercises_dir = '/Users/zhangzc/Desktop/workplace/daily-code-data/data/input/dailycode/leetcode/exercises' crawer_leetcode_dir = '/Users/zhangzc/Desktop/workplace/LeetCodeCN-Problem-Crawler/leetcode_html' dirs_ = os.listdir(data_dir) dirs = [] for dir in dirs_: dir = os.path.join(data_dir, dir) if os.path.isdir(dir): dirs.append(dir) for dir in dirs: assert os.path.isdir(dir) exercises_id = dir.split('/')[-1].split('_')[0] if 0 <= int(exercises_id) and int(exercises_id) < 100: desc_src_path = os.path.join(os.path.join(dailycode_exercises_dir, exercises_id), '{}_desc.html'.format(exercises_id)) cpp_code_src_path = os.path.join(os.path.join(dailycode_exercises_dir, exercises_id),'{}.cpp'.format(exercises_id)) desc_dst_path = os.path.join(dir, 'desc.html') cpp_code_dst_path = os.path.join(dir, 'solution.cpp') # print(cpp_code_src_path) # print(cpp_code_dst_path) shutil.copy(desc_src_path, desc_dst_path) shutil.copy(cpp_code_src_path, cpp_code_dst_path) else: cpp_code_dst_path = os.path.join(dir, 'solution.cpp') if not os.path.exists(cpp_code_dst_path): open(cpp_code_dst_path, 'w', encoding='utf-8') desc_src_path = os.path.join(crawer_leetcode_dir, str(int(exercises_id) + 1) + '.html') desc_dst_path = os.path.join(dir, 'desc.html') # print(desc_src_path) # print(desc_dst_path) if os.path.exists(desc_src_path): shutil.copy(desc_src_path, desc_dst_path) else: print("该路径不存在,请检查: {}".format(desc_src_path)) leetcode_helper()