内容简介:writelines() 方法用于向文件中写入一序列的字符串。
概述
writelines() 方法用于向文件中写入一序列的字符串。
这一序列字符串可以是由迭代对象产生的,如一个字符串列表。
换行需要制定换行符 \n。
语法
writelines() 方法语法如下:
fileObject.writelines( [ str ])
参数
- str -- 要写入文件的字符串序列。
返回值
该方法没有返回值。
实例
以下实例演示了 writelines() 方法的使用:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 打开文件
fo = open("test.txt", "w")
print "文件名为: ", fo.name
seq = ["www.codercto.com 1\n", "www.codercto.com 2"]
fo.writelines( seq )
# 关闭文件
fo.close()以上实例输出结果为:
文件名为: test.txt
查看文件内容:
$ cat test.txt www.codercto.com 1 www.codercto.com 2
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Machine Learning
Kevin Murphy / The MIT Press / 2012-9-18 / USD 90.00
Today's Web-enabled deluge of electronic data calls for automated methods of data analysis. Machine learning provides these, developing methods that can automatically detect patterns in data and then ......一起来看看 《Machine Learning》 这本书的介绍吧!