详解详解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中的多线程编程
Apr 09 Python
Python爬虫利用cookie实现模拟登陆实例详解
Jan 12 Python
Python外星人入侵游戏编程完整版
Mar 30 Python
Python中如何优雅的合并两个字典(dict)方法示例
Aug 09 Python
python3学生名片管理v2.0版
Nov 29 Python
Python神奇的内置函数locals的实例讲解
Feb 22 Python
python图形工具turtle绘制国际象棋棋盘
May 23 Python
python中时间、日期、时间戳的转换的实现方法
Jul 06 Python
python实现指定ip端口扫描方式
Dec 17 Python
在django中form的label和verbose name的区别说明
May 20 Python
Python接口自动化测试框架运行原理及流程
Nov 30 Python
matplotlib之多边形选区(PolygonSelector)的使用
Feb 24 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
解决File size limit exceeded 错误的方法
2013/06/14 PHP
CodeIgniter扩展核心类实例详解
2016/01/20 PHP
Zend Framework教程之Zend_Db_Table_Row用法实例分析
2016/03/21 PHP
Yii2实现跨mysql数据库关联查询排序功能代码
2017/02/10 PHP
JS 面向对象的5钟写法
2009/07/31 Javascript
javascript 面向对象编程 万物皆对象
2009/09/17 Javascript
Javascript对象中关于setTimeout和setInterval的this介绍
2012/07/21 Javascript
js实现数组去重、判断数组以及对象中的内容是否相同
2013/11/29 Javascript
纯JS实现本地图片预览的方法
2015/07/31 Javascript
JS延时提示框实现方法详解
2015/11/26 Javascript
Angular2表单自定义验证器的实现
2016/10/19 Javascript
JS实现图片垂直居中显示小结
2016/12/13 Javascript
使用JavaScript为一张图片设置备选路径的方法
2017/01/04 Javascript
H5手机端多文件上传预览插件
2017/04/21 Javascript
利用nodeJs anywhere搭建本地服务器环境的方法
2018/05/12 NodeJs
微信小程序使用for循环动态渲染页面操作示例
2018/12/25 Javascript
JavaScript原型对象原理与应用分析
2018/12/27 Javascript
在layui框架中select下拉框监听更改事件的例子
2019/09/20 Javascript
vue 自定义组件的写法与用法详解
2020/03/04 Javascript
JS字符串补全方法padStart()和padEnd()
2020/05/27 Javascript
浅谈vue中使用编辑器vue-quill-editor踩过的坑
2020/08/03 Javascript
python和C语言混合编程实例
2014/06/04 Python
python利用urllib和urllib2访问http的GET/POST详解
2017/09/27 Python
Pycharm远程调试openstack的方法
2017/11/21 Python
Python cookbook(数据结构与算法)在字典中将键映射到多个值上的方法
2018/02/18 Python
python tkinter界面居中显示的方法
2018/10/11 Python
使用django实现一个代码发布系统
2019/07/18 Python
Django为窗体加上防机器人的验证码功能过程解析
2019/08/14 Python
欧洲、亚洲、非洲和拉丁美洲的度假套餐:Great Value Vacations
2019/03/30 全球购物
法拉利英国精品店:Ferraris Boutique UK
2019/07/20 全球购物
屈臣氏菲律宾官网:Watsons菲律宾
2020/06/30 全球购物
挑战杯创业计划书的写作指南
2014/01/07 职场文书
倡议书格式范文
2014/04/14 职场文书
大学生英文求职信范文
2015/03/19 职场文书
贫困证明怎么写
2015/06/16 职场文书
Python first-order-model实现让照片动起来
2022/06/25 Python