详解详解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 Mysql自动备份脚本
Jul 14 Python
Python的几个高级语法概念浅析(lambda表达式闭包装饰器)
May 28 Python
Python zip()函数用法实例分析
Mar 17 Python
python高级特性和高阶函数及使用详解
Oct 17 Python
python学习开发mock接口
Apr 28 Python
selenium 安装与chromedriver安装的方法步骤
Jun 12 Python
基于Python的图像数据增强Data Augmentation解析
Aug 13 Python
Python字符串大小写转换拼接删除空白
Sep 19 Python
Python pathlib模块使用方法及实例解析
Oct 05 Python
python tkinter的消息框模块(messagebox,simpledialog)
Nov 07 Python
Python基础之字符串格式化详解
Apr 21 Python
Python if else条件语句形式详解
Mar 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
php遍历数组的方法分享
2012/03/22 PHP
PHP的curl实现get,post和cookie(实例介绍)
2013/06/17 PHP
PHP列出MySQL中所有数据库的方法
2015/03/12 PHP
PHP实现的文件上传类与用法详解
2017/07/05 PHP
浅谈PHP中如何实现Hook机制
2017/11/14 PHP
js 操作select相关方法函数
2009/12/06 Javascript
利用JQuery和JS实现奇偶行背景颜色自定义效果
2012/11/19 Javascript
javascript真的不难-回顾一下基础知识
2013/01/15 Javascript
使用js在页面中绘制表格核心代码
2013/09/16 Javascript
javascript scrollTop正解使用方法
2013/11/14 Javascript
Firefox中使用outerHTML的2种解决方法
2014/06/07 Javascript
使用 bootstrap modal遇到的问题小结
2016/11/09 Javascript
Agularjs妙用双向数据绑定实现手风琴效果
2017/05/26 Javascript
JSON数据中存在单个转义字符“\”的处理方法
2018/07/11 Javascript
vue后台管理之动态加载路由的方法
2018/08/13 Javascript
详解如何修改 node_modules 里的文件
2020/05/22 Javascript
原生JS实现pc端轮播图效果
2020/12/21 Javascript
[02:17]快乐加倍!DOTA2食人魔魔法师至宝+迎霜节活动上线
2019/12/22 DOTA
Python实现包含min函数的栈
2016/04/29 Python
使用Python自动化破解自定义字体混淆信息的方法实例
2019/02/13 Python
python求前n个阶乘的和实例
2020/04/02 Python
基于python实现查询ip地址来源
2020/06/02 Python
TensorFlow Autodiff自动微分详解
2020/07/06 Python
如何利用python之wxpy模块玩转微信
2020/08/17 Python
python模拟点击玩游戏的实例讲解
2020/11/26 Python
html5版canvas自由拼图实例
2014/10/15 HTML / CSS
HearthSong官网:儿童户外玩具、儿童益智玩具
2017/10/16 全球购物
美国高品质个性化珠宝销售网站:Jewlr
2018/05/03 全球购物
益模软件Java笔试题
2012/03/27 面试题
J2EE面试题集锦(附答案)
2013/08/16 面试题
硕士研究生个人求职信
2013/12/04 职场文书
军训结束新闻稿
2015/07/17 职场文书
2016教师读书思廉心得体会
2016/01/23 职场文书
初中生物教学反思
2016/02/20 职场文书
2016年社区植树节活动总结
2016/03/16 职场文书
Python之matplotlib绘制折线图
2022/04/13 Python