我正在尝试从Python编写一个txt文件:
for i in range(len(X)):
k+=1
g.write('CoordinatesX='+str(i)+str(X[i])+'\n')
g.write('D'+str(k)+'@Sketch'+str(sketch_number)+'=CoordinatesX'+str(k)+'\n')
k+=1
g.write('CoordinatesY='+str(i)+str(Y[i])+'\n')
g.write('D'+str(k)+'@Sketch'+str(sketch_number)+'=CoordinatesY'+str(k)+'\n')
k+=1
g.write('CoordinatesZ='+str(i)+str(Z[i])+'\n')
g.write('D'+str(k)+'@Sketch'+str(sketch_number)+'=CoordinatesZ'+str(k)+'\n')
g.close()我没有发现错误,但是当我去查找下载的文件时,我找不到它,也没有写任何东西。有人知道我做错了什么吗?已经谢谢你了。干杯!
发布于 2022-02-09 13:06:58
在Python中,您可以这样open一个文件:
file = open('file.txt', 'r+')
file.read() # This will return the content
file.write("This file has just been overwritten")
file.close() # This will close the file更好的做法是使用with/as语法:
with open('file.txt', 'r+') as file:
file.read()
file.write("This file has just been overwritten")
# The file is automatically closed (saved) after the with block has endedhttps://stackoverflow.com/questions/71050124
复制相似问题