python write( )函数

我们通过如下例子,示例一下write( )函数的使用方法:

write( )函数的参数是一个字符串,通过write()函数向文件中写入一行或多行:

#使用open()函数打开一个文本文件;

fd = open(r"d:\a.txt","a")

#使用write()函数往a.txt 文件中写入单行"Hello Python";

content=fd.write("Hello Python")
print()
print(content)

#使用write()函数往a.txt 文件中写入多行"Hello Python!\n You are Great.";

content=fd.write("\nHello Python! \nYou are Great.")
print(content)
print()

运行程序,结果如下:


原文链接:,转发请注明来源!