import os import re import shutil def process_files(cif_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"Percolation diameter \(A\): (\d+\.\d+)", content) # 使用正则表达式查找符合条件的内容 if matches: # 提取文件名(去掉.txt后缀) base_name = os.path.splitext(txt_file)[0] check = False if anion == "O": print(f"{base_name}的perconlation diameter为{matches[0]}A") if float(matches[0]) > 0.5: check = True print(f"符合要求") else: print("不符合要求") elif anion == "S": print(f"{base_name}的perconlation diameter为{matches[0]}A") if float(matches[0]) > 0.55: check = True print(f"符合要求") else: print("不符合要求") elif anion == "Br": print(f"{base_name}的perconlation diameter为{matches[0]}A") if float(matches[0]) > 0.45: check = True print("符合要求") else: print("不符合要求") elif anion == "Cl": print(f"{base_name}的perconlation diameter为{matches[0]}A") if float(matches[0]) > 0.45: check = True print("符合要求") else: print("不符合要求") if check: # 查找与 txt 文件同名的 cif 文件 cif_path = os.path.join(cif_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}") def work_py(input_folder, output_folder): if not os.path.exists(output_folder): print("not exists") for filename in os.listdir(input_folder): target_folder = os.path.join(output_folder, filename) from_folder = os.path.join(input_folder, filename) process_files(from_folder, target_folder) if __name__ == "__main__": # work_py("../data/after_step1","../data/after_step2" ) # process_files("../data/after_step1/O", "../data/after_step2/O", "O") # process_files("../data/after_step1/S", "../data/after_step2/S", "S") process_files("../data/after_step1/Cl", "../data/after_step2/Cl", "Br") process_files("../data/after_step1/Br", "../data/after_step2/Br", "Cl")