写入空文件
filename = 'programming.txt'
with open(filename,'w') as file_object:
file_object.write("I love programming.")
生成了txt的文件 programing
内容如下:
将第二个实参进行修订,由w→a,可以对原txt文件进行“附加模式”的内容添加
filename = 'programming.txt'
# with open(filename,'w') as file_object:
# file_object.write("I love Python.")
with open(filename,'a') as file_object:
file_object.write("C language is the next turn.")
使用换行符,实现在txt文件中的文字自动换行
filename = 'programming.txt'
with open(filename,'w') as f:
f.write("I love programming.\n")
f.write("I love creating new games.\n")
结论如下:
I love programming.
I love creating new games.