详解详解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实现斐波那契递归函数的方法
Sep 08 Python
跟老齐学Python之从if开始语句的征程
Sep 14 Python
详解Python中用于计算指数的exp()方法
May 14 Python
用Python设计一个经典小游戏
May 15 Python
python3 shelve模块的详解
Jul 08 Python
Django中使用celery完成异步任务的示例代码
Jan 23 Python
Python基于TCP实现会聊天的小机器人功能示例
Apr 09 Python
Python 中判断列表是否为空的方法
Nov 24 Python
tensorflow的ckpt及pb模型持久化方式及转化详解
Feb 12 Python
Anaconda+VSCode配置tensorflow开发环境的教程详解
Mar 30 Python
Python+Opencv身份证号码区域提取及识别实现
Aug 25 Python
全面介绍python中很常用的单元测试框架unitest
Dec 14 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实现var_export的详细介绍
2013/06/20 PHP
Codeigniter整合Tank Auth权限类库详解
2014/06/12 PHP
PHP Smarty模版简单使用方法
2016/03/30 PHP
php 的反射详解及示例代码
2016/08/25 PHP
js宝典学习笔记(上)
2007/01/10 Javascript
Add Formatted Text to a Word Document
2007/06/15 Javascript
文本框中禁止非数字字符输入比如手机号码、邮编
2013/08/19 Javascript
JS实现控制表格行内容垂直对齐的方法
2015/03/30 Javascript
Bootstrap的图片轮播示例代码
2015/08/31 Javascript
Vue.js每天必学之过渡与动画
2016/09/06 Javascript
Three.js如何实现雾化效果示例代码
2017/09/27 Javascript
浅谈JavaScript find 方法不支持IE的问题
2017/09/28 Javascript
vue2 全局变量的设置方法
2018/03/09 Javascript
jQuery发请求传输中文参数乱码问题的解决方案
2018/05/22 jQuery
Vue2.0点击切换类名改变样式的方法
2018/08/22 Javascript
Angular4.x Event (DOM事件和自定义事件详解)
2018/10/09 Javascript
javascript数组去重方法总结(推荐)
2019/03/20 Javascript
Vue 中获取当前时间并实时刷新的实现代码
2020/05/12 Javascript
[02:56]DOTA2亚洲邀请赛 VG出场战队巡礼
2015/02/07 DOTA
Python translator使用实例
2008/09/06 Python
python实现TF-IDF算法解析
2018/01/02 Python
Python计算库numpy进行方差/标准方差/样本标准方差/协方差的计算
2018/12/28 Python
Tensorflow 实现分批量读取数据
2020/01/04 Python
Django更新models数据库结构步骤
2020/04/01 Python
matplotlib基础绘图命令之imshow的使用
2020/08/13 Python
Django创建一个后台的基本步骤记录
2020/10/02 Python
在HTML5中如何使用CSS建立不可选的文字
2014/10/17 HTML / CSS
详解Canvas实用库Fabric.js使用手册
2019/01/07 HTML / CSS
凯特·丝蓓英国官网:Kate Spade英国
2016/11/07 全球购物
NIHAOMARKET官方海外旗舰店:意大利你好华人超市
2018/01/27 全球购物
TheFork葡萄牙:欧洲领先的在线餐厅预订平台
2019/05/27 全球购物
俄罗斯美容和健康网上商店:Созвездие Красоты
2019/07/23 全球购物
2014幼儿园教育教学工作总结
2014/12/17 职场文书
辞职书格式样本
2015/02/26 职场文书
2015年班组工作总结
2015/04/20 职场文书
如何用Navicat操作MySQL
2021/05/12 MySQL