详解详解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 相关文章推荐
Django卸载之后重新安装的方法
Mar 15 Python
python实现跨excel的工作表sheet之间的复制方法
May 03 Python
初探利用Python进行图文识别(OCR)
Feb 26 Python
Python中查看变量的类型内存地址所占字节的大小
Jun 26 Python
Python进阶:生成器 懒人版本的迭代器详解
Jun 29 Python
解决Python3用PIL的ImageFont输出中文乱码的问题
Aug 22 Python
python实现FTP文件传输的方法(服务器端和客户端)
Mar 20 Python
Python实现转换图片背景颜色代码
Apr 30 Python
浅谈keras2 predict和fit_generator的坑
Jun 17 Python
OpenCV 使用imread()函数读取图片的六种正确姿势
Jul 09 Python
理解Django 中Call Stack机制的小Demo
Sep 01 Python
Django框架模板用法详解
Jun 10 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/04/28 PHP
100行PHP代码实现socks5代理服务器
2016/04/28 PHP
ThinkPHP框架表单验证操作方法
2017/07/19 PHP
Javascript Boolean、Nnumber、String 强制类型转换的区别详细介绍
2012/12/13 Javascript
javascript parseInt() 函数的进制转换注意细节
2013/01/08 Javascript
JS判断不同分辨率调用不同的CSS样式文件实现思路及测试代码
2013/01/23 Javascript
Jquery多选下拉列表插件jquery multiselect功能介绍及使用
2013/05/24 Javascript
JavaScript对内存分配及管理机制详细解析
2013/11/11 Javascript
js的匿名函数使用介绍
2013/12/11 Javascript
一个网页标题title的闪动提示效果实现思路
2014/03/22 Javascript
jquery ajax 如何向jsp提交表单数据
2015/08/23 Javascript
jQuery中数据缓存$.data的用法及源码完全解析
2016/04/29 Javascript
JS表单数据验证的正则表达式(常用)
2017/02/18 Javascript
基于Vue中点击组件外关闭组件的实现方法
2018/03/06 Javascript
通过jquery获取上传文件名称、类型和大小的实现代码
2018/04/19 jQuery
vue.js iview打包上线后字体图标不显示解决办法
2020/01/20 Javascript
js实现删除json中指定的元素
2020/09/22 Javascript
原生JS运动实现轮播图
2021/01/02 Javascript
使用Python中的greenlet包实现并发编程的入门教程
2015/04/16 Python
详细讲解用Python发送SMTP邮件的教程
2015/04/29 Python
python使用WMI检测windows系统信息、硬盘信息、网卡信息的方法
2015/05/15 Python
利用Python学习RabbitMQ消息队列
2015/11/30 Python
python机器学习之KNN分类算法
2018/08/29 Python
浅谈python出错时traceback的解读
2020/07/15 Python
CSS3 实现的火焰动画
2020/12/07 HTML / CSS
Regatta官网:英国最受欢迎的户外服装和鞋类品牌
2019/05/01 全球购物
L’urv官网:精品女性运动服品牌
2019/07/07 全球购物
专业毕业生个性的自我评价
2013/10/03 职场文书
花店创业计划书范文
2014/02/07 职场文书
电子商务专业求职信
2014/07/10 职场文书
在职证明书范本(2014新版)
2014/09/25 职场文书
夫妻分居协议书范文
2014/11/26 职场文书
2015企业年终工作总结范文
2015/05/27 职场文书
百善孝为先:关于孝道的经典语录
2019/10/18 职场文书
nginx配置虚拟主机的详细步骤
2021/07/21 Servers
一次线上mongo慢查询问题排查处理记录
2022/03/18 MongoDB