使用 spyder 编写并保存一个 txt 文件
write and save a txt file using spyder
我正在尝试从 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()
我没有报错,但是当我去寻找下载的文件时,我没有找到它,也没有写任何东西。
有人知道我做错了什么吗?已经非常感谢了。
干杯!
在 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 ended
我正在尝试从 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()
我没有报错,但是当我去寻找下载的文件时,我没有找到它,也没有写任何东西。 有人知道我做错了什么吗?已经非常感谢了。 干杯!
在 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 ended