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

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

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

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

 write()方法把字符串str写入文件。没有返回值。由于缓冲,字符串可能不实际显示文件,直到flush()或close()方法被调用。
语法

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

fileObject.write( str )

</div>

参数

  •     str -- 这是要被写入的文件中的字符串。

返回值

此方法不返回任何值。
例子

下面的例子显示write()方法的使用。

#!/usr/bin/python

# Open a file in write 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

str = "This is 6th line"
# Write a line at the end of the file.
fo.seek(0, 2)
line = fo.write( str )

# Now read complete file from beginning.
fo.seek(0,0)
for index in range(6):
  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

</div>


</div>

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

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

相关文章

  • 使用Python中的cookielib模拟登录网站
  • Python实现批量将word转html并将html内容发布至网站的方法
  • Python中__init__和__new__的区别详解
  • Python中列表、字典、元组、集合数据结构整理
  • python中字典dict常用操作方法实例总结
  • python写的一个文本编辑器
  • python实现封装得到virustotal扫描结果
  • Python函数嵌套实例
  • 精确查找PHP WEBSHELL木马的方法(1)
  • pygame学习笔记(3):运动速率、时间、事件、文字

文章分类

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

最近更新的内容

    • 在Linux系统上部署Apache+Python+Django+MySQL环境
    • Python实现网站文件的全备份和差异备份
    • Python实现ssh批量登录并执行命令
    • python 实现网上商城,转账,存取款等功能的信用卡系统
    • Python实现将不规范的英文名字首字母大写
    • python利用拉链法实现字典方法示例
    • 举例讲解Python编程中对线程锁的使用
    • Python Web框架Flask下网站开发入门实例
    • Python中lambda的用法及其与def的区别解析
    • python通过urllib2获取带有中文参数url内容的方法

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

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