目錄
基本步驟
常用函數(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)存的使用。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-836426.html
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)!