韩漫免费漫画在线观看方法,《好好疼爱里面》免费看,年轻漂亮的女士护士内衣,妈妈醉酒后把我当爸爸电视剧

文章 > Python基础教程 > Python file read()方法

Python file read()方法

Python file read()

头像

宋雪维

2020-12-08 09:37:045141浏览 · 0收藏 · 0评论

在计算机中,文件包括了文档、图片、视频、程序组件等,每个类型的文件都有不同的作用或功用。例如一个程序通常由主程序、动态库、配置文件等组成,这些也是文件,起到支持程序运行的作用。想要使用文件,第一个操作就是打开读取文件,那么在python如何读取文件呢?其实使用python file read()方法。

描述

read()方法是Python的文件方法,用于读取文件中的内容,并返回文件内容的字符串。

语法

file.read(size)

返回值

读取文件,返回字符串类型的值。

 使用示例

1. size省略,一次性读完整个文件

待读取的文件 demo.txt:

2019

python代码:

data = open("demo.txt", "r").read()
print(data)

执行结果:

2019

2. 指定字节数读取文件

待读取的文件:demo.txt

A thread is a basic unit of CPU execution. It must depend on the process surviving. A thread is an execution context, which is what a CPU needs to execute
A list of instructions. In Python, multithreading takes longer.

假设我们只希望读取30字节的数据:

data = open("demo.txt", "r").read(30)
print(data)

执行结果如下:

A thread is a basic unit of CP

注意事项:

1.  size为负时

当size值为负数时read()方法不会报错,此时read()方法会读完整个文件。

待读取的文件:demo.txt

A thread is a basic unit of CPU execution. It must depend on the process surviving. A thread is an execution context, which is what a CPU needs to execute
A list of instructions. In Python, multithreading takes longer.

python脚本:

data = open("demo.txt", "r").read(-1)
print(data)

执行结果:

A thread is a basic unit of CPU execution. It must depend on the process surviving. A thread is an execution context, which is what a CPU needs to execute
A list of instructions. In Python, multithreading takes longer.

2. size为0时

当size等于0时,read方法返回一个空串。

data = open("demo.txt", "r").read(0)
print(data)
print(type(data))
print(len(data))

执行结果:

 
<class 'str'>
0

为何要使用Size?

当文件过大,内存不够一次性读取整个文件时,就需要分批读取文件。合理使用size可以妥善处理文件大于内存的场景。

文章来源于网络,如有雷同,请联系作者。

关注

关注公众号,随时随地在线学习

本教程部分素材来源于网络,版权问题联系站长!

欧美人动物PPT免费模板大全| 《XL司令》第一季动漫免费播放 | 《老公请部长家里吃饭》日剧| 《出差的日子》叶爱| 巨乳动漫| 日产精品一线二线三线芒果| 双人打扑克剧烈运动视频| 精品久久免费一区二区三区四区 | 母亲和女婿免费高清电视剧| 成版人黄瓜视频APP| 亲亲抱抱拔萝 卜| 孝子提枪入母门| 在车里被撞了八次高C| 美女两个球球抖动抓球球视频| 《出租屋1》| 高压监狱2法国1时43分| 强伦女教师2:伦理| 麻豆精品无码国产在线播放| 白日梦我小说全文免费阅读| 双飞娘俩国语版电视剧免费观看| 真人做爰45分钟| 男按摩师舌头伸进去了SPA漫画| 录像狗配女人| 《交换做爰》在线观看| 野外打野真实视频大全| 边摸边脱吃奶边高潮视频免费| 王叔 我老公还在客厅| 蒋舒含校花的YIN荡大学生活| 爸爸汆肉的日常| 巨乳动漫| 男男| 10岁小学生TEES幼儿| 桃色视频| 成品免费PPT网站入口| 白日梦我小说| 每天晚上都在汆肉中度| 欧美大片LOGO| 苍井空高清图片| 天美麻花高清在线看电视剧| 韩剧在办公室做饭的电影| 漫蛙漫画(网页入口)