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

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

关注

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

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

黄篇| 桃色禁区| 暴躁老阿姨CSGO技能推荐大全 | 亲嘴还扔衣服| 33D蜜桃成熟| 爱上兄弟的母亲6集免费观看| 中文字幕精品无码亚洲字幕一区| 王叔 我老公还在客厅| 公交车上把腿张开让人摸| 欧洲尺码少女16集全电视剧剧情..| 黑暗的反射| 《健身房的特殊待遇》小说| 被两个男人吃奶三P| 《帐篷里的秘密》第一季| 开会也一直放里面老板的| 皇空传媒天美传媒有限公司| 蜜桃成熟时2| 《互换人妻HD中字》| 把老婆闺蜜干了俩次| 妈妈的闺蜜2字ID| 电影《老公上班后》免费观看| 校服| 啵啵浏览器| 妻子的谎言片尾曲| 女性无裆连体内衣| 《酒店激战》动漫1-5集免费观.. 《帐篷里的秘密》动漫 | 短视频| 黄文| 美国伦理《轻佻寡妇》| 蜜臀| 灭火宝贝2:决战丛林| 黄瓜成视频人APP| 光谷有轨电车| 朝桐光| 初次尝了销魂少妇邻居 | 美国暴躁真人CSGO| 避孕套怎么用| 男朋友说我的骚B只能给他吃| 打扑克| 男生将78申请女生的定眼| 十二金鸭