python 示例_带有示例的Python File write()方法
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()方法相关推荐
- python 示例_带有示例的Python File read()方法
python 示例 文件read()方法 (File read() Method) read() method is an inbuilt method in Python, it is used t ...
- python示例_带有示例的Python功能指南
python示例 Python函数简介 (Introduction to Functions in Python) A function allows you to define a reusable ...
- python 示例_带有示例的Python File close()方法
python 示例 文件close()方法 (File close() Method) close() method is an inbuilt method in Python, it is use ...
- python 示例_带有示例的Python文件关闭属性
python 示例 文件关闭属性 (File closed Property) closed Property is an inbuilt property of File object (IO ob ...
- python 示例_带有示例的Python字典popitem()方法
python 示例 字典popitem()方法 (Dictionary popitem() Method) popitem() method is used to remove random/last ...
- python 示例_带有示例的Python列表remove()方法
python 示例 列出remove()方法 (List remove() Method) remove() method is used to remove the first occurrence ...
- lock_sh 示例_带有示例的Python date __str __()方法
lock_sh 示例 Python date .__ str __()方法 (Python date.__str__() Method) date.__str__() method is used t ...
- python 示例_带有示例的Python date timetuple()方法
python 示例 Python date.timetuple()方法 (Python date.timetuple() Method) date.timetuple() method is used ...
- python 示例_带有示例的Python date isocalendar()方法
python 示例 Python date.isocalendar()方法 (Python date.isocalendar() Method) date.isocalendar() method i ...
最新文章
- 新时代的网络工程师需要掌握哪些技能
- linux系统中怎么驱动U盘
- Docker核心原理之namespace
- NYOJ 137 取石子(三)
- 牛客网Java刷题知识点之字节缓冲区练习之从A处复制文本文件到B处(BufferedReader、BufferedWriter)、复制文本文件的原理图解...
- 一个关于组织学员学习技术的笔试题--求讨论
- WSS 3.0中An unexpected error has occurred的一个解决方案
- 【Python基础入门系列】第05天:Python函数
- numpy 归一化_NumPy 数据归一化、可视化
- 经典面试题(51):以下代码将输出的结果是什么?
- 【leetcode】1018. 可被 5 整除的二进制前缀
- 【Linux 4】定时任务调度与进程服务管理
- python基本类型关键字_python基本类型关键字_python中的关键字---1(基础数据类)...
- OpenGL三维变换与三维投影实例(行星绕日旋转)
- 合并请求格式太乱?工单内容各写各的?表单模板来帮你
- 前端面试总结以及面试题
- 对使用字符指针变量和字符数组的讨论
- Silvaco学习笔记(九)毕设相关
- 是堆内存分为年轻代和年老代!!!
- 电赛专题 | E题-互联网的信号传输