Python参考手册
Python File writelines() 方法
Python File writelines() 方法
Python File writelines() 方法
Python File(文件) 方法概述
writelines() 方法用于向文件中写入一序列的字符串。
这一序列字符串可以是由迭代对象产生的,如一个字符串列表。
换行需要制定换行符 \n。
语法
writelines() 方法语法如下:
fileObject.writelines( [ str ])
参数
-
str -- 要写入文件的字符串序列。
返回值
该方法没有返回值。
实例
以下实例演示了 writelines() 方法的使用:
#!/usr/bin/python # -*- coding: UTF-8 -*- # 打开文件 fo = open("test.txt", "w") print "文件名为: ", fo.name seq = ["非常教程 1\n", "非常教程 2"] fo.writelines( seq ) # 关闭文件 fo.close()
以上实例输出结果为:
文件名为: test.txt
查看文件内容:
$ cat test.txt 非常教程 1 非常教程 2Python File(文件) 方法
Python File writelines() 方法 |
---|
Python 是一种面向对象的解释型计算机程序设计语言,由荷兰人 Guido van Rossum 于1989年发明,第一个公开发行版发行于1991年。 Python 是纯粹的自由软件, 源代码和解释器 CPython 遵循 GPL 协议。Python 语法简洁清晰,特色之一是强制用空白符( white space )作为语句缩进。
主页 | https://www.python.org/ |
源码 | https://github.com/python/cpython |
版本 | 2.7 |
发布版本 | 2.7.13 |