Editing single line in a file using python. Last part of new file is missing
I am trying to edit a single line in a povray file using python.
The file starts out like so:
camera {
angle 38
location <500,0,0>
right x*image_width/image_height
look_at开发者_开发知识库 <0,0,0>
rotate <0,0,0>
}
and I want to edit the variables described in the file so it comes out like this:
camera {
angle 38
location <1000,1000,1000>
right x*image_width/image_height
look_at <10,10,10>
rotate <30,30,30>
}
To do this I am using the following method:
def updateCamera(self, filename):
tmp = "povrayContent/temp.pov"
lines = open(filename, 'r')
out = open(tmp , 'w')
for line in lines:
if " angle" in line:
out.write(" angle "+str(int(self.camAngle))+"\n")
elif " location" in line:
out.write(" location <"+str(int(self.camera[0]))+","+str(int(self.camera[1]))+","+str(int(self.camera[2]))+">\n")
elif " look_at" in line:
out.write(" look_at <"+str(int(self.camera[3]))+","+str(int(self.camera[4]))+","+str(int(self.camera[5]))+">\n")
elif "rotate" in line:
out.write(" rotate <"+str(int(self.camera[6]))+","+str(int(self.camera[7]))+","+str(int(self.camera[8]))+">\n")
else:
out.write(line)
shutil.copy(tmp, filename)
EDIT: Camera is a tuple that contains the new values the variables should be changed to. For the most part this seems to work. The only problem is that after the camera declaration there are about 40k lines of other declarations of objects in the scene. The problem I am having is that the last 20 or so lines of declarations are missing from the new file. Does anybody know what is going on, is there some limitation of python I am unaware of?
Either flush or close the temporary file before copying to be certain that all output has been written to disk.
精彩评论