python按列合并两个等长txt文件

该博客主要介绍了如何使用Python脚本来合并pdb和topo文件,并在合并过程中进行文件路径操作。代码中定义了一个遍历指定目录下所有pdb_id的文件,将pdb文件和对应的topo文件内容按行合并,并保存为新的pdb文件。这个过程对于处理蛋白质结构数据或分子模拟研究可能非常有用。
摘要由CSDN通过智能技术生成

   

                                    (1.txt)                                                                     (2.txt)

'''
# !/usr/bin/python3
# -*- coding: utf-8 -*-
@Time : 2021/11/22 19:11
@Author : Qiufen.Chen
@FileName: distance.py
@Software: PyCharm
'''
import os

input_path = '/home/chenqiufen/ContactMap/ContactMap/data/topo_2/'
for (root, dirs, files) in os.walk(input_path):  # 列出windows目录下的所有文件和文件名
    file_num = 0
    for file_name in files:
        pdb_id = file_name.split('.')[0]

        topo_file = os.path.join(root) + '/' + file_name
        pdb_file = os.path.join('/home/chenqiufen/ContactMap/data/NR_chainAtom/', pdb_id + '.pdb')
        save_path = os.path.join('/home/chenqiufen/ContactMap/ContactMap/data/atom_topo/', pdb_id + '.pdb')

        file1 = open(pdb_file, "r", encoding="utf-8")
        file2 = open(topo_file, "r", encoding="utf-8")
        file3 = open(save_path, "w", encoding="utf-8")

        while True:
            mystr1 = file1.readline()  # 表示一次读取一行
            mystr2 = file2.readline()
            if not mystr1:
                # 读到数据最后跳出,结束循环。数据的最后也就是读不到数据了,mystr为空的时候
                break
                
            file3.write(mystr1[:-1])
            file3.write("\t")
            file3.write(mystr2)

        file1.close()
        file3.close()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值