加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

使用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)

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

解决方法

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读