详解详解Python中writelines()方法的使用


Posted in Python onMay 25, 2015

 writelines()方法写入字符串序列到文件。该序列可以是任何可迭代的对象产生字符串,字符串为一般列表。没有返回值。
语法

以下是writelines()方法的语法:

fileObject.writelines( sequence )

参数

  •     sequence -- 这是字符串的序列。

返回值

此方法不返回任何值。
例子

下面的例子显示writelines()方法的使用。

#!/usr/bin/python'

# Open a file in witre mode
fo = open("foo.txt", "rw+")
print "Name of the file: ", fo.name

# Assuming file has following 5 lines
# This is 1st line
# This is 2nd line
# This is 3rd line
# This is 4th line
# This is 5th line

seq = ["This is 6th line\n", "This is 7th line"]
# Write sequence of lines at the end of the file.
fo.seek(0, 2)
line = fo.writelines( seq )

# Now read complete file from beginning.
fo.seek(0,0)
for index in range(7):
  line = fo.next()
  print "Line No %d - %s" % (index, line)

# Close opend file
fo.close()

当我们运行上面的程序,它会产生以下结果:

Name of the file: foo.txt
Line No 0 - This is 1st line

Line No 1 - This is 2nd line

Line No 2 - This is 3rd line

Line No 3 - This is 4th line

Line No 4 - This is 5th line

Line No 5 - This is 6th line

Line No 6 - This is 7th line

Python 相关文章推荐
python多进程操作实例
Nov 21 Python
Python 遍历子文件和所有子文件夹的代码实例
Dec 21 Python
Python实现Pig Latin小游戏实例代码
Feb 02 Python
让Django支持Sql Server作后端数据库的方法
May 29 Python
详解python项目实战:模拟登陆CSDN
Apr 04 Python
django自带serializers序列化返回指定字段的方法
Aug 21 Python
利用pandas合并多个excel的方法示例
Oct 10 Python
3分钟看懂Python后端必须知道的Django的信号机制
Jul 26 Python
用Python实现定时备份Mongodb数据并上传到FTP服务器
Jan 27 Python
Python用requests库爬取返回为空的解决办法
Feb 21 Python
python 管理系统实现mysql交互的示例代码
Dec 06 Python
Python万能模板案例之matplotlib绘制直方图的基本配置
Apr 13 Python
Python中操作文件之write()方法的使用教程
May 25 #Python
在Python中操作文件之truncate()方法的使用教程
May 25 #Python
Python中tell()方法的使用详解
May 24 #Python
在Python中操作文件之seek()方法的使用教程
May 24 #Python
简单介绍Python中的readline()方法的使用
May 24 #Python
在Python中操作文件之read()方法的使用教程
May 24 #Python
在Python中使用next()方法操作文件的教程
May 24 #Python
You might like
一个可以删除字符串中HTML标记的PHP函数
2006/10/09 PHP
php中的登陆login
2007/01/18 PHP
PHP simple_html_dom.php+正则 采集文章代码
2009/12/24 PHP
Yii入门教程之目录结构、入口文件及路由设置
2014/11/25 PHP
PHP魔术方法的使用示例
2015/06/23 PHP
thinkPHP批量删除的实现方法分析
2016/11/09 PHP
formvalidator验证插件中有关ajax验证问题
2013/01/04 Javascript
通过JavaScript使Div居中并随网页大小改变而改变
2013/06/24 Javascript
Jquery操作radio的简单实例
2014/01/06 Javascript
JS获取select的value和text值的简单实例
2014/02/26 Javascript
json格式数据的添加,删除及排序方法
2016/01/21 Javascript
JavaScript中Object.prototype.toString方法的原理
2016/02/24 Javascript
jQuery xml字符串的解析、读取及查找方法
2016/03/01 Javascript
jQuery实现页面倒计时并刷新效果
2017/03/13 Javascript
JavaScript之json_动力节点Java学院整理
2017/06/29 Javascript
如何解决React官方脚手架不支持Less的问题(小结)
2018/09/12 Javascript
详解如何使用微信小程序云函数发送短信验证码
2019/03/13 Javascript
[04:10]2016国际邀请赛中国区预选赛第二日TOP10精彩集锦
2016/06/28 DOTA
python使用datetime模块计算各种时间间隔的方法
2015/03/24 Python
Python中if __name__ == '__main__'作用解析
2015/06/29 Python
python实现在字符串中查找子字符串的方法
2015/07/11 Python
Python删除windows垃圾文件的方法
2015/07/14 Python
Python3 log10()函数简单用法
2019/02/19 Python
Python importlib动态导入模块实现代码
2020/04/16 Python
python可以用哪些数据库
2020/06/22 Python
python中spy++的使用超详细教程
2021/01/29 Python
使用CSS3在触屏上为按钮实现激活效果
2013/09/27 HTML / CSS
英国时尚饰品和发饰购物网站:Claire’s
2017/07/04 全球购物
用C或者C++语言实现SOCKET通信
2015/02/24 面试题
大学生自我评价怎样写好
2013/10/23 职场文书
会计专业的自荐信
2013/12/12 职场文书
棉花姑娘教学反思
2014/02/15 职场文书
小学竞选班长演讲稿
2014/09/09 职场文书
三好学生个人总结
2015/02/15 职场文书
公司开业致辞
2015/07/29 职场文书
《刷子李》教学反思
2016/02/20 职场文书