使用python编辑文件中的单行.缺少新文件的最后一部分
发布时间:2020-12-20 11:19:06 所属栏目: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 rig
我正在尝试使用
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) 编辑:相机是一个元组,包含变量应更改为的新值. 解决方法
在复制之前刷新或关闭临时文件,以确保所有输出都已写入磁盘.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |