根据Excel表格对文件夹内文件重命名

D:\1CAAS\Lab\songqianlin\Cas新蛋白\Cas12\cas12_lmnopq_fasta\重复项去除\对文件夹重命名.py

点击查看代码
'''能不能根据
PLV_Cas_operons_putative_protein_candidates_published_filtered.xlsx
表格里面的TnpB这一个附表里面的No和protein_id这一列
对PDB结构文件夹里面一系列用protein_id命名的PDB文件重命名为No
'''
import os
import pandas as pd

# Excel文件
excel_file = r"D:\1CAAS\Lab\生物信息操作\结构域划分\PLV\PLV_Cas_operons_putative_protein_candidates_published_filtered.xlsx"

# PDB文件夹
pdb_dir = r"D:\1CAAS\Lab\songqianlin\folddisco\PLV_TnpB_folddisco(1)\PV_TnpB_PDB"

# 读取TnpB工作表
df = pd.read_excel(excel_file, sheet_name="TnpB")

# 去除空值
df = df[["No", "protein_id"]].dropna()

rename_count = 0
not_found = []

for _, row in df.iterrows():
    no = str(row["No"]).strip()
    protein_id = str(row["protein_id"]).strip()

    old_file = os.path.join(pdb_dir, f"{protein_id}.pdb")
    new_file = os.path.join(pdb_dir, f"{no}.pdb")

    if os.path.exists(old_file):
        os.rename(old_file, new_file)
        rename_count += 1
        print(f"{protein_id}.pdb -> {no}.pdb")
    else:
        not_found.append(protein_id)

print(f"\n成功重命名: {rename_count}")

if not_found:
    print("\n未找到对应PDB文件:")
    for x in not_found:
        print(x)
posted @ 2026-09-03 17:23  Zarinan  阅读(4)  评论(0)    收藏  举报