小白教程
所有教程
关于
Search
172.69.58.90
172.69.58.90
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“Python3 file read”的源代码
本页内容
上一节:
Python3_file_next
下一节:
Python3_file_readline
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:file.read([size])}}[[Category:python3 file methods|6]] = Python3 File read() 方法 = [[python3 file methods|Python3 File(文件) 方法]] === 概述 === '''read()''' 方法用于从文件读取指定的字符数(文本模式 t)或字节数(二进制模式 b),如果未给定参数 '''size''' 或 '''size''' 为负数则读取文件所有内容。 === 语法 === read() 方法语法如下: <sample title="" desc="" lang="python" hererun="1"> fileObject.read([size]); </sample> === 参数 === * '''size''' -- 从文件中读取的字符数(文本模式)或字节数(二进制模式),默认为 '''-1''',表示读取整个文件。 === 返回值 === 返回从字符串中读取的字节。 == 以下实例演示了 read() 方法的使用: 文件 xiaobai.txt 的内容如下: <sample title="" desc="" lang="python" hererun="1"> 这是第一行 这是第二行 这是第三行 这是第四行 这是第五行 </sample> 循环读取文件的内容: <sample title="" desc="" lang="python" hererun="1"> #!/usr/bin/python3 # 打开文件 fo = open("xiaobai.txt", "r+") print ("文件名为: ", fo.name) line = fo.read(10) print ("读取的字符串: %s" % (line)) # 关闭文件 fo.close() </sample> 以上实例输出结果为: <sample title="" desc="" lang="python" hererun="1"> 文件名为: xiaobai.txt 读取的字符串: 这是第一行 这是第二 </sample> [[python3 file methods|Python3 File(文件) 方法]]
返回至“
Python3 file read
”。
上一节:
Python3_file_next
下一节:
Python3_file_readline