国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

python讀寫(xiě)文件操作的三大基本步驟

這篇具有很好參考價(jià)值的文章主要介紹了python讀寫(xiě)文件操作的三大基本步驟。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

目錄

基本步驟

常用函數(shù)

open()函數(shù)

close()函數(shù)

read()函數(shù)

readlines()函數(shù)

readline()函數(shù)

write()函數(shù)

writelines()函數(shù)

with語(yǔ)句

讀寫(xiě)操作的應(yīng)用:

拷貝文件

?with 語(yǔ)句的嵌套

逐行拷貝


基本步驟

1. 打開(kāi)文件:open(filepath, mode, encoding)

2. 讀寫(xiě)文件:read() / write()

3. 關(guān)閉文件:close()

python讀取文件操作實(shí)例
f = open('filename.txt', 'r', encoding='utf-8')
f.read()
f.close()

常用函數(shù)

open()函數(shù)

open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)

file是要打開(kāi)的文件,mode='r'是打開(kāi)文件的模式,encoding是編碼格式

文件的打開(kāi)模式有以下字符表示:

? ? 'r' ? ? ? open for reading (default)
? ? 'w' ? ? ? open for writing, truncating the file first
? ? 'x' ? ? ? create a new file and open it for writing
? ? 'a' ? ? ? open for writing, appending to the end of the file if it exists
? ? 'b' ? ? ? binary mode
? ? 't' ? ? ? text mode (default)
? ? '+' ? ? ? open a disk file for updating (reading and writing)

打開(kāi)模式還能連用:

The default mode is 'rt' (open for reading text). For binary random? access, the mode 'w+b' opens and truncates the file to 0 bytes, while?'r+b' opens the file without truncation. The 'x' mode implies 'w' and?raises an `FileExistsError` if the file already exists.

close()函數(shù)

close() method of _io.TextIOWrapper instance
? ? Flush and close the IO object.

? ? This method has no effect if the file is already closed

打開(kāi)文件,必須有對(duì)應(yīng)的關(guān)閉,否則該未關(guān)閉的文件不能被其它的應(yīng)用操作。

read()函數(shù)

read(size=-1, /) method of _io.TextIOWrapper instance
? ? Read at most size characters from stream.

? ? Read from underlying buffer until we have size characters or we hit EOF.
? ? If size is negative or omitted, read until EOF.

read()可選參數(shù)size可以用于讀取“size”大小的數(shù)據(jù),返回的是字符串或字節(jié)對(duì)象,若是size的值沒(méi)有填寫(xiě),或者是個(gè)負(fù)值,那么read()函數(shù)將讀取文件的所有內(nèi)容,這也是用python開(kāi)發(fā)pc軟件中“復(fù)制黏貼”的功能比較常用的函數(shù)。

readlines()函數(shù)

Help on built-in function readlines:

readlines(hint=-1, /) method of _io.TextIOWrapper instance
? ? Return a list of lines from the stream.

? ? hint can be specified to control the number of lines read: no more
? ? lines will be read if the total size (in bytes/characters) of all
? ? lines so far exceeds hint.

readlines()函數(shù)是將文件當(dāng)中的所有行,一行一行地讀取,并逐一寫(xiě)入一個(gè)列表list內(nèi),最終返回這個(gè)列表。

readline()函數(shù)

Help on built-in function readline:

readline(size=-1, /) method of _io.TextIOWrapper instance
? ? Read until newline or EOF.

? ? Return an empty string if EOF is hit immediately.
? ? If size is specified, at most size characters will be read.

readline()函數(shù)就是讀取一行數(shù)據(jù),用法除了size參數(shù)之外,就跟read()差不多,也是open()打開(kāi)文件,readline()讀取數(shù)據(jù),close()關(guān)閉文件

write()函數(shù)

write(text, /) method of _io.TextIOWrapper instance
? ? Write string s to stream.

? ? Return the number of characters written
? ? (which is always equal to the length of the string).

writelines()函數(shù)

writelines(lines, /) method of _io.TextIOWrapper instance
? ? Write a list of lines to stream.

? ? Line separators are not added, so it is usual for each of the
? ? lines provided to have a line separator at the end.

with語(yǔ)句

通過(guò)with語(yǔ)句,不管讀取還是寫(xiě)入文件操作都不用寫(xiě)對(duì)應(yīng)的close()函數(shù),語(yǔ)句塊結(jié)束系統(tǒng)會(huì)自動(dòng)關(guān)閉文件。

with open('filename.txt', 'r', encoding='urf-8') as f:
? ? f.read()

讀寫(xiě)操作的應(yīng)用:

拷貝文件

# 打開(kāi)源文件以讀取內(nèi)容  
with open('source.txt', 'r') as source_file:  
    source_content = source_file.read()  
  
# 打開(kāi)目標(biāo)文件以寫(xiě)入內(nèi)容  
with open('destination.txt', 'w') as destination_file:  
    destination_file.write(source_content)

?with 語(yǔ)句的嵌套

以上兩個(gè)with語(yǔ)句塊還能嵌套寫(xiě)在一起:

# 打開(kāi)源文件以讀取內(nèi)容,并同時(shí)打開(kāi)目標(biāo)文件以寫(xiě)入內(nèi)容  
with open('source.txt', 'r') as source_file:  
    with open('destination.txt', 'w') as destination_file:  
        # 讀取源文件的內(nèi)容  
        source_content = source_file.read()  
        # 將讀取的內(nèi)容寫(xiě)入到目標(biāo)文件中  
        destination_file.write(source_content)

可以寫(xiě)在同一行:

# 打開(kāi)源文件以讀取內(nèi)容,并同時(shí)打開(kāi)目標(biāo)文件以寫(xiě)入內(nèi)容  
with open('source.txt', 'r') as source_file, open('destination.txt', 'w') as destination_file:  
    # 讀取源文件的內(nèi)容  
    source_content = source_file.read()  
    # 將讀取的內(nèi)容寫(xiě)入到目標(biāo)文件中  
    destination_file.write(source_content)

逐行拷貝

如果源文件很大,使用read()方法一次性讀取所有內(nèi)容可能會(huì)消耗較多的內(nèi)存。對(duì)于大文件,更推薦的做法是使用文件對(duì)象的迭代器逐行讀取和寫(xiě)入,這樣可以減少內(nèi)存的使用。

with open('source.txt', 'r') as source_file, open('destination.txt', 'w') as destination_file:  
    # 逐行讀取源文件并寫(xiě)入目標(biāo)文件  
    for line in source_file:  
        destination_file.write(line)

文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-836426.html

到了這里,關(guān)于python讀寫(xiě)文件操作的三大基本步驟的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包