python 示例

文件write()方法 (File write() Method)

write() method is an inbuilt method in Python, it is used to write the content in the file.

write()方法是Python中的内置方法,用于将内容写入文件中。

Syntax:

句法:

    file_object.write(text/bytes)

Parameter(s):

参数:

  • text/bytes – Specifies the text to be written in the file.

    文本/字节 –指定要写入文件的文本。

Return value:

返回值:

The return type of this method is <class 'int'>, it returns the total number of the written in the file.

此方法的返回类型为<class'int'> ,它返回文件中写入的总数。

Example:

例:

# Python File write() Method with Example
# creating a file
myfile = open("hello.txt", "w")
# writing to the file
res = myfile.write("Hello friends, how are you?")
print(res, "bytes written to the file.")
# closing the file
myfile.close()
# reading content from the file
myfile = open("hello.txt", "r")
print("file content is...")
print(myfile.read())
myfile.close();
# writing more content to the file
# opening file in append mode
myfile = open("hello.txt", "a")
# writing to the file
res = myfile.write("Hey, I am good!")
print(res, "bytes written to the file.")
# closing the file
myfile.close()
# reading content from the file again
myfile = open("hello.txt", "r")
print("file content is...")
print(myfile.read())
myfile.close()

Output

输出量

27 bytes written to the file.
file content is...
Hello friends, how are you?
15 bytes written to the file.file content is...
Hello friends, how are you?Hey, I am good!

翻译自: https://www.includehelp.com/python/file-write-method-with-example.aspx

python 示例

python 示例_带有示例的Python File write()方法相关推荐

  1. python 示例_带有示例的Python File read()方法

    python 示例 文件read()方法 (File read() Method) read() method is an inbuilt method in Python, it is used t ...

  2. python示例_带有示例的Python功能指南

    python示例 Python函数简介 (Introduction to Functions in Python) A function allows you to define a reusable ...

  3. python 示例_带有示例的Python File close()方法

    python 示例 文件close()方法 (File close() Method) close() method is an inbuilt method in Python, it is use ...

  4. python 示例_带有示例的Python文件关闭属性

    python 示例 文件关闭属性 (File closed Property) closed Property is an inbuilt property of File object (IO ob ...

  5. python 示例_带有示例的Python字典popitem()方法

    python 示例 字典popitem()方法 (Dictionary popitem() Method) popitem() method is used to remove random/last ...

  6. python 示例_带有示例的Python列表remove()方法

    python 示例 列出remove()方法 (List remove() Method) remove() method is used to remove the first occurrence ...

  7. lock_sh 示例_带有示例的Python date __str __()方法

    lock_sh 示例 Python date .__ str __()方法 (Python date.__str__() Method) date.__str__() method is used t ...

  8. python 示例_带有示例的Python date timetuple()方法

    python 示例 Python date.timetuple()方法 (Python date.timetuple() Method) date.timetuple() method is used ...

  9. python 示例_带有示例的Python date isocalendar()方法

    python 示例 Python date.isocalendar()方法 (Python date.isocalendar() Method) date.isocalendar() method i ...

最新文章

  1. 新时代的网络工程师需要掌握哪些技能
  2. linux系统中怎么驱动U盘
  3. Docker核心原理之namespace
  4. NYOJ 137 取石子(三)
  5. 牛客网Java刷题知识点之字节缓冲区练习之从A处复制文本文件到B处(BufferedReader、BufferedWriter)、复制文本文件的原理图解...
  6. 一个关于组织学员学习技术的笔试题--求讨论
  7. WSS 3.0中An unexpected error has occurred的一个解决方案
  8. 【Python基础入门系列】第05天:Python函数
  9. numpy 归一化_NumPy 数据归一化、可视化
  10. 经典面试题(51):以下代码将输出的结果是什么?
  11. 【leetcode】1018. 可被 5 整除的二进制前缀
  12. 【Linux 4】定时任务调度与进程服务管理
  13. python基本类型关键字_python基本类型关键字_python中的关键字---1(基础数据类)...
  14. OpenGL三维变换与三维投影实例(行星绕日旋转)
  15. 合并请求格式太乱?工单内容各写各的?表单模板来帮你
  16. 前端面试总结以及面试题
  17. 对使用字符指针变量和字符数组的讨论
  18. Silvaco学习笔记(九)毕设相关
  19. 是堆内存分为年轻代和年老代!!!
  20. 电赛专题 | E题-互联网的信号传输

热门文章

  1. 原生js、jQuery实现选项卡功能
  2. HTML5标签用法及描述
  3. 布局中常见的居中问题
  4. 移动端判断手机横竖屏状态
  5. 吉首大学2019年程序设计竞赛-F 天花乱坠
  6. C# -- 多线程向同一文件写入
  7. 独立线性度 最佳直线
  8. 三、 将DataTable 转换为List
  9. 关于代码手写UI,xib和StoryBoard
  10. YUI 的模块信息配置优先级关系梳理