使用python编辑文件中的单行新文件的最后一部分丢失

问题描述

| 我正在尝试使用python编辑povray文件中的一行。 文件开始如下:
camera {
 angle 38
 location <500,0>
 right x*image_width/image_height
 look_at <0,0>
 rotate <0,0>
}
而且我想编辑文件中描述的变量,这样它就会出来:
camera {
 angle 38
 location <1000,1000,1000>
 right x*image_width/image_height
 look_at <10,10,10>
 rotate <30,30,30>
}
为此,我使用以下方法
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)
编辑:相机是一个元组,其中包含变量应更改为的新值。 在大多数情况下,这似乎可行。唯一的问题是,在摄像机声明之后,场景中还有大约40k行其他对象声明。我遇到的问题是新文件中缺少最后20行左右的声明。有人知道发生了什么吗,我不知道python有什么限制吗?     

解决方法

        复制前请刷新或关闭临时文件,以确保所有输出均已写入磁盘。