簡體   English   中英

python 僅將第一行寫入文件

[英]python writing only first line to the file

我的腳本只將第一行寫入文件。 我檢查了前面相同的問題,他們的問題是在 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')

還有這個:

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')

在這兩種方式中,它都只寫第一行。 Output 是這樣的:

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

python 的新用戶,希望得到一些幫助:)

問題是您正在為每一行打開文件,當您使用w打開文件時,文件的先前內容將被刪除。

只打開一次文件:

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')

工作版本:

#!/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