简体   繁体   English

python 仅将第一行写入文件

[英]python writing only first line to the file

My script is writing only the first line to the file.我的脚本只将第一行写入文件。 I checked the previous same questions, problem with them was writing to the file after the for loop so i tried both this:我检查了前面相同的问题,他们的问题是在 for 循环之后写入文件,所以我尝试了这两个:

import os
import sys
import pdb_atoms as atms
import numpy as np

path_to_ligands=('/home/user/Desktop/small_test/ligands/')
path_to_CMS=('/home/user/Desktop/small_test/CMS/')


with os.scandir(path_to_ligands) as lig:
    for each_file in lig:       
        text=open(each_file, "r")
        lines=text.readlines()
        CMS_coord= np.array( atms.CMS(each_file) )  #getting the coordinates of each atoms center of masses
        for line in lines:
            x =[]
            x.append( float(line[30:38]) - CMS_coord[0])
            x.append( float(line[38:46]) - CMS_coord[1])
            x.append( float(line[46:54]) - CMS_coord[2])
            line = line.strip()
            text_to_write=[line[:30] + ("%8.3f%8.3f%8.3f" % (x[0],x[1],x[2])) +line[54:]]
        new_name = path_to_CMS + each_file.name.replace('ligand', 'lig_CMS')
        with open(new_name, 'w') as w:
            for i in text_to_write:
                w.write(i + '\n')

and also this:还有这个:

path_to_ligands=('/home/user/Desktop/small_test/ligands/')
path_to_CMS=('/home/user/Desktop/small_test/CMS/')


with os.scandir(path_to_ligands) as lig:
    for each_file in lig:       
        text=open(each_file, "r")
        lines=text.readlines()
        CMS_coord= np.array( atms.CMS(each_file) )  #getting the coordinates of each atoms center of masses
        for line in lines:
            x =[]
            x.append( float(line[30:38]) - CMS_coord[0])
            x.append( float(line[38:46]) - CMS_coord[1])
            x.append( float(line[46:54]) - CMS_coord[2])
            line = line.strip()
            text_to_write=[line[:30] + ("%8.3f%8.3f%8.3f" % (x[0],x[1],x[2])) +line[54:]]
            new_name = path_to_CMS + each_file.name.replace('ligand', 'lig_CMS')
            with open(new_name, 'w') as w:
                for i in text_to_write:
                    w.write(i + '\n')

in both ways it is writing only the first line.在这两种方式中,它都只写第一行。 Output is this: Output 是这样的:

ATOM     36  C28 VFL L 288       2.449  -2.116   0.546  1.00 15.00      L    C

New at python, would appreciate some help:) python 的新用户,希望得到一些帮助:)

The problem is that you are opening the file for each line, when you open the file with w the previous contents of the file are erased.问题是您正在为每一行打开文件,当您使用w打开文件时,文件的先前内容将被删除。

Open the file only once:只打开一次文件:

import os
import sys
import pdb_atoms as atms
import numpy as np

path_to_ligands=('/home/user/Desktop/small_test/ligands/')
path_to_CMS=('/home/user/Desktop/small_test/CMS/')


with os.scandir(path_to_ligands) as lig:
    for each_file in lig:       
        new_name = path_to_CMS + each_file.name.replace('ligand', 'lig_CMS')
        with open(new_name, 'w') as w:
            text=open(each_file, "r")
            lines=text.readlines()
            CMS_coord= np.array( atms.CMS(each_file) )  #getting the coordinates of each atoms center of masses
            for line in lines:
                x =[]
                x.append( float(line[30:38]) - CMS_coord[0])
                x.append( float(line[38:46]) - CMS_coord[1])
                x.append( float(line[46:54]) - CMS_coord[2])
                line = line.strip()
                text_to_write=[line[:30] + ("%8.3f%8.3f%8.3f" % (x[0],x[1],x[2])) +line[54:]]
            for i in text_to_write:
                w.write(i + '\n')

Working version:工作版本:

#!/usr/bin/python3

import os
import sys
import pdb_atoms as atms
import numpy as np

path_to_ligands=('/home/user/Desktop/small_test/ligands/')
path_to_CMS=('/home/user/Desktop/small_test/CMS/')


with os.scandir(path_to_ligands) as lig:
    for each_file in lig:       
        new_name = path_to_CMS + each_file.name.replace('ligand', 'lig_CMS')
        with open(new_name, 'w') as w:
            text=open(each_file, "r")
            lines=text.readlines()
            CMS_coord= np.array( atms.CMS(each_file) )  #getting the coordinates of each atoms center of masses
            for line in lines:
                x =[]
                x.append( float(line[30:38]) - CMS_coord[0])
                x.append( float(line[38:46]) - CMS_coord[1])
                x.append( float(line[46:54]) - CMS_coord[2])
                line = line.strip()
                text_to_write=line[:30] + ("%8.3f%8.3f%8.3f" % (x[0],x[1],x[2])) +line[54:]
                w.write(text_to_write + '\n')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM