博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
带有示例的Python File readline()方法
阅读量:2530 次
发布时间:2019-05-11

本文共 2630 字,大约阅读时间需要 8 分钟。

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

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

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

Syntax:

句法:

file_object.readline(bytes)

Parameter(s):

参数:

  • bytes – 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 the whole line.

    bytes –这是一个可选参数,可用于指定要从文件读取的总字节数。 它的默认值为-1,用于指定整行。

Return value:

返回值:

The return type of this method is <class 'str'>, it returns the string.

该方法的返回类型为<class'str'> ,它返回字符串。

Example 1:

范例1:

# Python File readline() Method with Example# creating a filemyfile1 = open("hello1.txt", "w")# writing content in the filemyfile1.write("Shivang, 21, Indore\n")myfile1.write("Pankaj, 27, Mumbai\n")myfile1.write("Rambha, 16, Heaven\n")# closing the filemyfile1.close()# reading the file (opening file in 'r' mode)myfile1 = open("hello1.txt","r")# reading and printing the file's content  # line by lineprint("file's content (using readline() method)...")print("line1: ", myfile1.readline())print("line2: ", myfile1.readline())print("line3: ", myfile1.readline())# reading and printing the file's content# all at once using read() method# seeking the file position at 0th positionmyfile1.seek(0)print("file's content (using read() method)...")print(myfile1.read())# closing the filemyfile1.close()

Output

输出量

file's content (using readline() method)...line1:  Shivang, 21, Indoreline2:  Pankaj, 27, Mumbailine3:  Rambha, 16, Heavenfile's content (using read() method)...Shivang, 21, IndorePankaj, 27, MumbaiRambha, 16, Heaven

Example 2:

范例2:

# Python File readline() Method with Example# creating a filemyfile1 = open("hello1.txt", "w")# writing content in the filemyfile1.write("Shivang, 21, Indore\n")myfile1.write("Pankaj, 27, Mumbai\n")myfile1.write("Rambha, 16, Heaven\n")# closing the filemyfile1.close()# reading the file (opening file in 'r' mode)myfile1 = open("hello1.txt","r")# reading and printing the file's content  # line by lineprint("file's content (using readline() method)...")# reads whole lineprint("line1: ", myfile1.readline(-1))# reads 5 bytesprint("line2: ", myfile1.readline(5))# reads next 10 bytesprint("line3: ", myfile1.readline(10))# closing the filemyfile1.close()

Output

输出量

file's content (using readline() method)...line1:  Shivang, 21, Indoreline2:  Pankaline3:  j, 27, Mum

翻译自:

转载地址:http://bxtzd.baihongyu.com/

你可能感兴趣的文章
JAVA 基础 / 第八课:面向对象 / JAVA类的方法与实例方法
查看>>
Ecust OJ
查看>>
P3384 【模板】树链剖分
查看>>
Thrift源码分析(二)-- 协议和编解码
查看>>
考勤系统之计算工作小时数
查看>>
4.1 分解条件式
查看>>
Equivalent Strings
查看>>
flume handler
查看>>
收藏其他博客园主写的代码,学习加自用。先表示感谢!!!
查看>>
H5 表单标签
查看>>
su 与 su - 区别
查看>>
C语言编程-9_4 字符统计
查看>>
在webconfig中写好连接后,在程序中如何调用?
查看>>
限制用户不能删除SharePoint列表中的条目(项目)
查看>>
【Linux网络编程】使用GDB调试程序
查看>>
feign调用spring clound eureka 注册中心服务
查看>>
ZT:Linux上安装JDK,最准确
查看>>
LimeJS指南3
查看>>
关于C++ const成员的一些细节
查看>>
《代码大全》学习摘要(五)软件构建中的设计(下)
查看>>