日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:Pythonfileread()方法

在計算機中,文件包括了文檔、圖片、視頻、程序組件等,每個類型的文件都有不同的作用或功用。例如一個程序通常由主程序、動態(tài)庫、配置文件等組成,這些也是文件,起到支持程序運行的作用。想要使用文件,第一個操作就是打開讀取文件,那么在python如何讀取文件呢?其實使用Python file read()()方法。

我們一直強調(diào)成都網(wǎng)站制作、成都做網(wǎng)站、外貿(mào)營銷網(wǎng)站建設(shè)對于企業(yè)的重要性,如果您也覺得重要,那么就需要我們慎重對待,選擇一個安全靠譜的網(wǎng)站建設(shè)公司,企業(yè)網(wǎng)站我們建議是要么不做,要么就做好,讓網(wǎng)站能真正成為企業(yè)發(fā)展過程中的有力推手。專業(yè)網(wǎng)絡(luò)公司不一定是大公司,創(chuàng)新互聯(lián)公司作為專業(yè)的網(wǎng)絡(luò)公司選擇我們就是放心。

描述

read()方法是Python的文件方法,用于讀取文件中的內(nèi)容,并返回文件內(nèi)容的字符串。

語法

file.read(size)

返回值

讀取文件,返回字符串類型的值。

 使用示例

1. size省略,一次性讀完整個文件

待讀取的文件 demo.txt:

2019

python代碼:

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

執(zhí)行結(jié)果:

2019

2. 指定字節(jié)數(shù)讀取文件

待讀取的文件: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.

假設(shè)我們只希望讀取30字節(jié)的數(shù)據(jù):

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

執(zhí)行結(jié)果如下:

A thread is a basic unit of CP

注意事項:

1.  size為負(fù)時

當(dāng)size值為負(fù)數(shù)時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)

執(zhí)行結(jié)果:

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時

當(dāng)size等于0時,read方法返回一個空串。

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

執(zhí)行結(jié)果:

 

0

為何要使用Size?

當(dāng)文件過大,內(nèi)存不夠一次性讀取整個文件時,就需要分批讀取文件。合理使用size可以妥善處理文件大于內(nèi)存的場景。

文章來源于網(wǎng)絡(luò),如有雷同,請聯(lián)系作者。


當(dāng)前標(biāo)題:創(chuàng)新互聯(lián)Python教程:Pythonfileread()方法
轉(zhuǎn)載源于:http://www.5511xx.com/article/dheeeep.html