讀寫是Python中常見的操作富俄, 通過讀寫請求操作系統(tǒng)打開文件對象
禁炒,然后讀取數(shù)據(jù)或者寫入數(shù)據(jù)。
1. 讀文件
-
f.read([size])
方式霍比,讀取指定size的字節(jié)數(shù)幕袱,如果未給定或為負則讀取所有
with open('file.txt', 'r+') as f:
print(f.read())
#testing
2020-01-25Python 3.9.0a3 now available for testing
2020-01-17Start using 2FA and API tokens on PyPI
2020-01-07Python Software Foundation Fellow Members for Q4 2019
read() 是一次性讀取文件的全部內(nèi)容,即一次性把文件加載到內(nèi)存悠瞬, 如果文件很大们豌, 超出內(nèi)存范圍涯捻,會報錯MemoryError
。 所以需要讀取指定字節(jié)數(shù)
望迎。下面只是用了小文件舉例障癌。
DATA_SIZE=20
with open('file.txt', 'r+') as f:
while True:
block = f.read(DATA_SIZE)
if block:
print(block)
else:
break
#testing
2020-01-25Python 3.9
.0a3 now available f
or testing
2020-01-1
7Start using 2FA and
API tokens on PyPI
2020-01-07Python Sof
tware Foundation Fel
low Members for Q4 2
019
-
f.readline()
方式,讀取整行辩尊,包括換行符
with open('file.txt', 'r+') as f:
while True:
line = f.readline()
if line:
print(line)
else:
break
#testing
2020-01-25Python 3.9.0a3 now available for testing
2020-01-17Start using 2FA and API tokens on PyPI
2020-01-07Python Software Foundation Fellow Members for Q4 2019
-
f.readlines()
方式涛浙,讀取所有行并返回list
with open('file.txt', 'r+') as f:
print(f.readlines())
#testing
['2020-01-25Python 3.9.0a3 now available for testing\n', '2020-01-17Start using 2FA and API tokens on PyPI\n', '2020-01-07Python Software Foundation Fellow Members for Q4 2019']
-
文件對象f
當作迭代對象,系統(tǒng)將自動處理IO緩沖和內(nèi)存管理摄欲, 這種方法是更加pythonic的方法
with open('file.txt', 'r+') as f:
for line in f:
print(line)
# testing
2020-01-25Python 3.9.0a3 now available for testing
2020-01-17Start using 2FA and API tokens on PyPI
2020-01-07Python Software Foundation Fellow Members for Q4 2019
總結(jié):
f.read()
和 f.readlines()
方法都是一次性讀取文件全部內(nèi)容轿亮,操作比較方便,但是對于大文件蒿涎,會出現(xiàn)內(nèi)存溢出等問題哀托,所以需要使用f.read(size)
和 文件對象f
的方法。
2. 快速無誤的讀取大文件
- 一個比較
pythonic
的方法劳秋,把文件對象f
當作迭代對象
實現(xiàn):打開文件的過程中仓手,不會一次性讀取全部文件,而是采用每次讀取一行的方式玻淑。
優(yōu)缺點:代碼簡潔嗽冒,如果一行數(shù)據(jù)大小超過內(nèi)存,也會造成MemoryError
补履。
with open('file.txt', 'r+') as f:
for line in f:
print(line)
- 分割數(shù)據(jù)+yield
實現(xiàn):將文件切分成小段添坊,每次處理完小段內(nèi)容后,釋放內(nèi)存
優(yōu)缺點:代碼略顯復雜箫锤,讀取大文件一般沒問題
def read_big_file(filename, size=4096):
with open(filename, 'r+') as f:
while True:
block = f.read(size)
if block:
yield block
else:
break
for block in read_big_file('file.txt'):
print(block)
# testing
2020-01-25Python 3.9.0a3 now available for testing
2020-01-17Start using 2FA and API tokens on PyPI
2020-01-07Python Software Foundation Fellow Members for Q4 2019
3. 寫文件
-
f.write()
方式贬蛙, 將字符串寫入文件,返回的是寫入的字符長度
with open('file.txt', 'w+') as f:
f.write('Hello!\n')
# testing
cat file.txt
Hello!
-
f.writelines()
方式谚攒, 向文件寫入一個序列字符串列表阳准,如果需要換行則要每行加入行符
with open('file.txt', 'w+') as f:
f.writelines(['Hello\n', 'World\n'])
# testing
cat file.txt
Hello
World
總結(jié):
寫文件操作是比較簡單的,但是一定要記住馏臭,寫入文件后要關(guān)閉文件
野蝇。
寫文件時,操作系統(tǒng)為提高效率括儒,往往不會立刻把數(shù)據(jù)寫入磁盤绕沈,而是放到內(nèi)存緩存起來,空閑的時候再慢慢寫入帮寻。只有調(diào)用close()方法時乍狐,操作系統(tǒng)才保證把沒有寫入的數(shù)據(jù)全部寫入磁盤。
一旦程序崩潰规婆, 可能會導致中途寫入的數(shù)據(jù)丟失澜躺, 所以推薦使用with open
函數(shù)蝉稳。