详解详解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实现的DES加密算法和3DES加密算法实例
Jun 03 Python
Python中逗号的三种作用实例分析
Jun 08 Python
python爬取w3shcool的JQuery课程并且保存到本地
Apr 06 Python
Python基于回溯法子集树模板实现图的遍历功能示例
Sep 05 Python
如何利用Boost.Python实现Python C/C++混合编程详解
Nov 08 Python
python logging模块的使用总结
Jul 09 Python
python 模拟银行转账功能过程详解
Aug 06 Python
PyTorch中Tensor的维度变换实现
Aug 18 Python
Django实现基于类的分页功能
Oct 31 Python
在脚本中单独使用django的ORM模型详解
Apr 01 Python
Python趣味挑战之教你用pygame画进度条
May 31 Python
Python matplotlib安装以及实现简单曲线的绘制
Apr 26 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
jQuery 名称冲突的解决方法
2011/04/08 Javascript
使用jQuery中的when实现多个AJAX请求对应单个回调的例子分享
2014/04/23 Javascript
js数组的操作指南
2014/12/28 Javascript
javascript中判断json的方法总结
2015/08/27 Javascript
使用jQuery制作基础的Web图片轮播效果
2016/04/22 Javascript
Bootstrap基本样式学习笔记之标签(5)
2016/12/07 Javascript
vue.js移动端app之上拉加载以及下拉刷新实战
2017/09/11 Javascript
使用vue-router完成简单导航功能【推荐】
2018/06/28 Javascript
JavaScript简单实现关键字文本搜索高亮显示功能示例
2018/07/25 Javascript
浅析Vue.js 中的条件渲染指令
2018/11/19 Javascript
Vue 表情包输入组件的实现代码
2019/01/21 Javascript
NodeJs操作MongoDB教程之分页功能以及常见问题
2019/04/09 NodeJs
Vue实现导航栏的显示开关控制
2019/11/01 Javascript
小程序实现上下切换位置
2020/11/16 Javascript
[06:16]第十四期-国士无双绝地翻盘之撼地神牛
2014/06/24 DOTA
小结Python用fork来创建子进程注意事项
2014/07/03 Python
python安装Scrapy图文教程
2017/08/14 Python
Python中的浮点数原理与运算分析
2017/10/12 Python
对Python通过pypyodbc访问Access数据库的方法详解
2018/10/27 Python
django组合搜索实现过程详解(附代码)
2019/08/06 Python
python中字典按键或键值排序的实现代码
2019/08/27 Python
numpy:找到指定元素的索引示例
2019/11/26 Python
python 字段拆分详解
2019/12/17 Python
解决Python命令行下退格,删除,方向键乱码(亲测有效)
2020/01/16 Python
Python实现遗传算法(二进制编码)求函数最优值方式
2020/02/11 Python
Python基础类继承重写实现原理解析
2020/04/03 Python
PyQt5-QDateEdit的简单使用操作
2020/07/12 Python
html5+css3之制作header实例与更新
2020/12/21 HTML / CSS
交通安全演讲稿
2014/01/07 职场文书
酒店总经理欢迎词
2014/01/15 职场文书
学校综治宣传月活动总结
2014/07/02 职场文书
我与祖国共奋进演讲稿
2014/09/13 职场文书
法院授权委托书格式
2014/09/28 职场文书
不同意离婚答辩状
2015/05/22 职场文书
勇敢的心观后感
2015/06/09 职场文书
上帝为你开了一扇窗之Tkinter常用函数详解
2021/06/02 Python