import os import re import shutil def process_files(cif_folder,input_folder,output_folder, anion): # 确保输出文件夹存在 os.makedirs(output_folder, exist_ok=True) # 获取 txt 文件夹中的所有 txt 文件 txt_files = [f for f in os.listdir(cif_folder) if f.endswith('.txt')] # 遍历 txt 文件 for txt_file in txt_files: txt_path = os.path.join(cif_folder, txt_file) # 打开并读取 txt 文件内容 with open(txt_path, 'r', encoding='utf-8') as file: content = file.read() matches = re.findall(r"the minium of d\s+([\d\.]+)\s*#", content) # 使用正则表达式查找符合条件的内容 if matches: # 提取文件名(去掉.txt后缀) base_name = os.path.splitext(txt_file)[0] check = False if anion == "O": print(f"{base_name}的最短距离为{matches[0]}A") if float(matches[0]) < 3: check = True print(f"符合要求") else: print("不符合要求") elif anion == "S": print(f"{base_name}的最短距离为{matches[0]}A") if float(matches[0]) < 3: check = True print(f"符合要求") else: print("不符合要求") elif anion == "Cl": print(f"{base_name}的最短距离为{matches[0]}A") if float(matches[0]) < 3: check = True print(f"符合要求") else: print("不符合要求") elif anion == "Br": print(f"{base_name}的最短距离为{matches[0]}A") if float(matches[0]) < 3: check = True print(f"符合要求") else: print("不符合要求") if check: # 查找与 txt 文件同名的 cif 文件 cif_path = os.path.join(input_folder, base_name) # 如果对应的 cif 文件存在,复制到 output_folder if os.path.exists(cif_path): shutil.copy(cif_path, os.path.join(output_folder, base_name)) print(f"Copied {base_name} to {output_folder}") if __name__ == '__main__': # process_files("../data/after_step1/O","../data/after_step2/O", "../data/after_step3/O", "O") # process_files("../data/after_step1/S", "../data/after_step2/S","../data/after_step3/S", "S") process_files("../data/after_step1/Cl", "../data/after_step2/Cl","../data/after_step3/Cl", "Cl") process_files("../data/after_step1/Br", "../data/after_step2/Br","../data/after_step3/Br", "Br")