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

readlines() method is an inbuilt method in Python, it is used to get all lines from the file, the method is called with this object (current file stream/IO object) and returns all available lines in the file, we can also specify the total number of bytes to read from the line.

readlines()方法是Python中的内置方法,用于从文件中获取所有行,该方法与此对象(当前文件流/ IO对象)一起调用,并返回文件中所有可用的行,我们还可以指定从行读取的总字节数。

Syntax:

句法:

    file_object.readlines(len)

Parameter(s):

参数:

  • len – It is an optional parameter and it can be used to specify the total number of bytes to read from the file. It's default value is -1 that specifies all lines. If the len is greater than the total number of bytes of the file, then no more content will return.

    len –这是一个可选参数,可用于指定要从文件读取的总字节数。 默认值为-1,表示所有行。 如果len大于文件的字节总数,则不会再返回任何内容。

Return value:

返回值:

The return type of this method is <class 'list'>, it returns the lines in the form of a list.

此方法的返回类型为<class'list'> ,它以列表形式返回行。

Example:

例:

# Python File readlines() Method with Example
# creating a file
myfile1 = open("hello1.txt", "w")
# writing content in the file
myfile1.write("Shivang, 21, Indore\n")
myfile1.write("Pankaj, 27, Mumbai\n")
myfile1.write("Rambha, 16, Indraloka\n")
myfile1.write("Urvarshi, 18, Indraloka\n")
myfile1.write("Menaka, 17, Indraloka\n")
# closing the file
myfile1.close()
# reading the file (opening file in 'r' mode)
myfile1 = open("hello1.txt","r")
# reading and printing the file's content
# using readlines()
print("file's content (using readlines() method)...")
print("myfile1.readlines()...")
print(myfile1.readlines())
# reading a total number of bytes
# seeking file's position to 0th position
myfile1.seek(0)
# reads only 10 bytes
print("myfile1.readlines(10)...")
print(myfile1.readlines(10))
# reads next 300 bytes, if no more bytes
# method will not read more bytes
print("myfile1.readlines(300)...")
print(myfile1.readlines(300))
# closing the file
myfile1.close()

Output

输出量

file's content (using readlines() method)...
myfile1.readlines()...
['Shivang, 21, Indore\n', 'Pankaj, 27, Mumbai\n', 'Rambha, 16,Indraloka\n', 'Urvarshi, 18, Indraloka\n', 'Menaka, 17, Indraloka\n']
myfile1.readlines(10)...
['Shivang, 21, Indore\n']
myfile1.readlines(300)...
['Pankaj, 27, Mumbai\n', 'Rambha, 16, Indraloka\n', 'Urvarshi,18, Indraloka\n', 'Menaka, 17, Indraloka\n']

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

带有示例的Python File readlines()方法相关推荐

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

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

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

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

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

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

  4. flush python_带有示例的Python File flush()方法

    flush python 文件flush()方法 (File flush() Method) flush() method is an inbuilt method in Python, it is ...

  5. isatty_带有示例的Python File isatty()方法

    isatty 文件isatty()方法 (File isatty() Method) isatty() method is an inbuilt method in Python, it is use ...

  6. 带有示例的Python File readline()方法

    文件readline()方法 (File readline() Method) readline() method is an inbuilt method in Python, it is used ...

  7. file.fileno_带有示例的Python File fileno()方法

    file.fileno 文件fileno()方法 (File fileno() Method) fileno() method is an inbuilt method in Python, it i ...

  8. python file readlines_Python File readlines() 方法

    Python File readlines() 方法 概述 readlines() 方法用于读取所有行(直到结束符 EOF)并返回列表,若给定sizeint>0,返回总和大约为sizeint字节 ...

  9. python3 readlines的参数_Python3 File readlines() 方法

    Python3 File readlines() 方法 概述 readlines() 方法用于读取所有行(直到结束符 EOF)并返回列表,该列表可以由 Python 的 for... in ... 结 ...

最新文章

  1. js里的匿名函数 数组排序
  2. Python面向对象中反射和双下的正确用法
  3. 前端学习(5):深入了解网站开发
  4. matlab地址数据类型uns,使用matlab生成sine波mif文件
  5. MongoDB警告信息
  6. rman 脚本备份全过程
  7. 什么叫显示动力学_通过流体动力学研究找到制作煎饼的最佳方法
  8. 【Java从0到架构师】项目实战 - 驾考(旧)- Freemarker、MyBatis-Plus
  9. silverlight3:(ItemControl 的)UI Virtualization
  10. 使用 Segue 傳送選取的 Table Cell 內容到下一個 View
  11. ul列表中包含input时line-height属性失效的解决办法
  12. lrzsz的交叉编译与使用
  13. 服务器晚上自动重启是什么原因,服务器经常自动重启是什么原因
  14. 如何禁止NavigationController的向右滑动返回
  15. Internal: Invoking ptxas not supported on WindowsRelying on driver to perform ptx compilation.
  16. 【U8+】U8手工增加会计期间提示找不到存储过程
  17. 【深度学习】YOLOv7速度精度超越其他变体,大神AB发推,网友:还得是你!|开源...
  18. 浪潮各机型管理芯片BMC IP(智能平台管理接口)设置
  19. B站直播,哔哩哔哩虚拟主播数据如何分析?
  20. 前端导出(JSON\数据流)

热门文章

  1. gmat阅读.html,GMAT阅读长难句50句+参考译文.pdf
  2. setTimeout(fn,0)
  3. CSS3新增的伪类选择器
  4. CCS3的过渡、变换、动画以及响应式布局、弹性布局
  5. Hibernate关联映射(一对多/多对多)
  6. web安全之CSRF
  7. bzoj4709 [Jsoi2011]柠檬
  8. js中的this指针(二)
  9. windows下写的脚本,在linux下执行失败
  10. mysql单表索引个数_MySQL性能:多个表与单个表和分区上的索引