赞
踩
Python writelines写文件教程
在
writelines写文件详解
语法
file.writelines(string)
参数
参数
描述
file
文件对象。
string
要写入的文件内容。
说明
file 表示已经打开的文件对象,string 表示要写入文件的
writelines 函数名虽然是 writelines,但是需要注意的是 writelines 函数写完一行之后,并不会主动换行,如果需要主动换行,还是需要我们手动加 “\n”。
案例
使用writelines函数写文件
使用 write 函数向已打开的文件写入内容
print("嗨客网(www.haicoder.net)")
file = open("C:/haicoder.txt", "w")
file.writelines("Hello HaiCoder")
file.writelines("Hello HaiCoder")
print("file write success")
file.close()
程序运行后,控制台输出如下:
我们使用 open 函数以写
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。