国产成人AV无码一二三区,少女1到100集,国产精品久久久久精品综合紧,巜公妇之诱感肉欲HD在线播放

文章 > 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可以妥善处理文件大于内存的场景。

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

关注

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

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

小妹妹爱大棒棒免费观看电视剧动.. | 在浴室和男朋友站着做好爽| 教室停电插班花赵柔笔趣阁| 白日梦我小说全文免费阅读| 儿子控制不住对妈妈发脾气怎么办| 老赵在货车把儿媳妇送到了| 4399在线观看免费高清电视剧| 儿子双飞妈妈和女儿的小说| 上海吃奶门| 人犬兽人高清版在线观看| 《厨房激战5》完整版在线观看| 鲁鲁射击免费播放电视剧全部 | 《赤裸奶头》完整版| 《请好好疼爱里面》免费观看| 日韩精品无码免费专区| 军官(巨肉高H)| 女人乱子伦xXX沧州| 亚洲国产精品99久久久久久| 98在线高清免费观看电视剧狂| 暴躁少女零九CSGO| 翁虹《玉尺经》| 姐姐片多多免费高清电视剧| 三女片免费观看电视剧杨贵妃传| 床上108种扦插方法| 韩国三级《偷妻》| 黑色董丝包臀紧身连衣裙| 医生含着我的奶边摸边做视频| 黑色莆丝透视露胸旗袍连衣裙| 《互换人妻HD中字》| 《丰满的女邻居》播放| 自述吃了春晚药后有多疯狂| 我把英语老师按在桌子吵了一顿| 久久无码亚洲成A人片| 缘之空第一季在线观看免费完整番. | 老翁玩嫩媳| 公与媳2电视剧免费| 接电话顶C的说不出话PLAY| 日韩大片PPT免费PPT| 万能浏览器| 小峡子的味道4| 白日梦我小说全文免费阅读