• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > 详解详解Python中writelines()方法的使用

详解详解Python中writelines()方法的使用

作者:goldensun 字体:[增加 减小] 来源:互联网

goldensun 通过本文主要向大家介绍了python writelines,python f.writelines,writelines,f.writelines,r语言writelines等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

 writelines()方法写入字符串序列到文件。该序列可以是任何可迭代的对象产生字符串,字符串为一般列表。没有返回值。
语法

以下是writelines()方法的语法:

fileObject.writelines( sequence )

</div>

参数

  •     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()

</div>

当我们运行上面的程序,它会产生以下结果:

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

</div>


</div>

您可能想查找下面的文章:

  • 详解详解Python中writelines()方法的使用
  • Python中操作文件之write()方法的使用教程

相关文章

  • 详解Django通用视图中的函数包装
  • Python通过PIL获取图片主要颜色并和颜色库进行对比的方法
  • Windows下PyMongo下载及安装教程
  • python optparse模块使用实例
  • python处理json数据中的中文
  • Python HTTP客户端自定义Cookie实现实例
  • Python脚本处理空格的方法
  • Python利用ansible分发处理任务
  • 浅谈Python的垃圾回收机制
  • Python实现查找系统盘中需要找的字符

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • python使用arp欺骗伪造网关的方法
    • Python多层嵌套list的递归处理方法(推荐)
    • 浅谈function(函数)中的动态参数
    • python简单的函数定义和用法实例
    • Windows系统下使用flup搭建Nginx和Python环境的方法
    • 详解Python中的文件操作
    • Python 专题一 函数的基础知识
    • python实现带验证码网站的自动登陆实现代码
    • 全面了解Python的getattr(),setattr(),delattr(),hasattr()
    • Python标准库06之子进程 (subprocess包) 详解

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有