python write( )函数

(交流学习群 467604262)

在博客(python 本地数据获取   网址:http://blog.csdn.net/sxingming/article/details/51333663)中,我们详细介绍了python中文件读写的各种方法。

本文通过例子具体示例一下write( )函数的使用。

write( )函数的参数是一个字符串,分以下2种情况:

1》通过write()函数向文件中写入一行

>>> f=open(r"C:\Users\Administrator\Desktop\test.txt",'w')
>>> f.write('hello,world!\n')#写入的字符串仅仅在末尾包含一个换行符。
>>> f.close()

运行程序,结果如下:

2》通过write()函数向文件中写入多行

>>> f=open(r"C:\Users\Administrator\Desktop\test.txt",'w')
>>> f.write('hello python!\nhello world!\n')#写入的字符串包含多个换行符,可以达到写入多行的效果
>>> f.close()

运行程序,结果如下:

猜你喜欢

转载自blog.csdn.net/xixi20200/article/details/108953893