python 示例

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

read() method is an inbuilt method in Python, it is used to read the content of the file, by using this method we can read the specified number of bytes from the file or content of the whole file.

read()方法是Python中的内置方法,用于读取文件的内容,通过使用此方法,我们可以从文件或整个文件的内容中读取指定数目的字节。

Syntax:

句法:

    file_object.read(size)

Parameter(s):

参数:

  • size – It is an optional parameter, it specifies the number of bytes to be read from the file. It's default value is -1 that returns the content of the whole file.

    size –这是一个可选参数,它指定要从文件读取的字节数。 它的默认值是-1 ,它返回整个文件的内容。

Return value:

返回值:

The return type of this method is <class 'str'>, it returns the string i.e. file's content (if the file is in text mode).

此方法的返回类型为<class'str'> ,它返回字符串,即文件的内容(如果文件处于文本模式)。

Example:

例:

# Python File read() Method with Example
# creating a file
myfile = open("hello.txt", "w")
# wrting text to the file
myfile.write("C++ is a popular programming language.")
# closing the file
myfile.close()
# reading the file i.e. opening file in read mode
myfile = open("hello.txt", "r")
# reading & printing the whole file
# Here, we are not specifying the size
print("myfile.read()...")
print(myfile.read())
# reset the position
myfile.seek(0)
# reading 10 bytes and printing
print("myfile.read(10)...")
print(myfile.read(10))
# reset the position
myfile.seek(0)
# reading whole file by passing -1
print("myfile.read(-1)...")
print(myfile.read(-1))
# closing the file
myfile.close()

Output

输出量

myfile.read()...
C++ is a popular programming language.
myfile.read(10)...
C++ is a p
myfile.read(-1)...
C++ is a popular programming language.

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

python 示例

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

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

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

  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. go select 与 for 区别_Go 面试每天一篇(第 102 天)
  2. group by是否会用到索引_平时工作中经常用到的SQL,这些你都知道吗?
  3. Python:列表、集合等交集、并集、差集、非集简介及其代码实现之详细攻略
  4. Unity架构有点乱
  5. emui10如何降级_如何评价EMUI10按包名降分辨率机制的行为?
  6. hdu4011(水贪心)
  7. catia怎么将特征参数化_搭建商城网站怎么将页面简洁化?这三个步骤不能少
  8. 磁盘管理之逻辑卷管理(Logical Volume Manager)
  9. ode45 matlab 出错,请问,Matlab用ODE45解微分方程,出错
  10. [Vue-cli3] is a Vue CLI 3 only command and you are using Vue CLI 2.9.6. You may...
  11. pip 换清华源 更新所有库 windows
  12. 中文分词:正向最大匹配与逆向最大匹配
  13. mysql--id主键列乱了之后,重新排序
  14. 科普一下: 关于C语言中的运算符优先级
  15. win10安装Hive3.0.0
  16. 二、T100库存杂收、杂发、报废管理篇
  17. Java Swing 开发总结汇总贴
  18. 华为HCIA认证考试简介
  19. Git-远程仓库【转】
  20. Javascript中append和appendChild有什么不同?

热门文章

  1. python 怎么将数组转为列表_怎么将视频转为GIF动态图 表情包怎么制作
  2. linux+cp+-rdf,简单构建基于RDF和SPARQL的KBQA(知识图谱问答系统)
  3. 宇轩网络面试题目PHP,二十道接地气的php面试题,让你直接通过面试!就此奉上~...
  4. 阿酷快捷键怎么使用_必须收藏!Linux用户必须知道的常用终端快捷键
  5. 字典排序什么意思_字典排序问题
  6. 为什么redis取出来是null_[2020] Redis 最新面试题
  7. 接口里面的方法都是抽象方法吗_大家都在讲高中学习的方法有哪些,那方法和技巧有什么异同的吗?...
  8. html5语义化标签 加粗,html5标签 H5标签
  9. 4n35光耦引脚图_光耦继电器的主要特点以及输入特性!先进光半导体
  10. Scala变量和常用数据类型