gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 13:23:22 25 4
gpt4 key购买 nike

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

文件开头是这样的:

camera {
angle 38
location <500,0,0>
right x*image_width/image_height
look_at <0,0,0>
rotate <0,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 限制?

最佳答案

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

关于python - 使用 python 编辑文件中的单行。新文件的最后一部分丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6347494/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com