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:
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:
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:)
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.
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')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.