使用python编辑文件中的单行.缺少新文件的最后一部分

Aar*_*n S 3 python file

我正在尝试使用python编辑povray文件中的单行.

该文件如下所示:

camera {
 angle 38
 location <500,0,0>
 right x*image_width/image_height
 look_at <0,0,0>
 rotate <0,0,0>
}
Run Code Online (Sandbox Code Playgroud)

我想编辑文件中描述的变量,所以它出现如下:

camera {
 angle 38
 location <1000,1000,1000>
 right x*image_width/image_height
 look_at <10,10,10>
 rotate <30,30,30>
}
Run Code Online (Sandbox Code Playgroud)

为此,我使用以下方法:

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)
Run Code Online (Sandbox Code Playgroud)

编辑:相机是一个元组,包含变量应更改为的新值.在大多数情况下,这似乎有效.唯一的问题是在摄像机声明之后,场景中有大约40k行的其他对象声明.我遇到的问题是新文件中缺少最后20行左右的声明.有谁知道发生了什么,是否有一些我不知道的python限制?

Ign*_*ams 7

在复制之前刷新或关闭临时文件,以确保所有输出都已写入磁盘.