python學習之檔案高階處理作業

2022-06-12 13:12:12 字數 3256 閱讀 5380

1)第一種方法

src_file = input('請輸入原始檔的路徑:').strip()

dst_file = input('請輸入目標檔案的路徑:').strip()

with open(r'{}'.format(src_file),'rb') as f1,\

open(r'{}'.format(dst_file),'wb') as f2:

content = f1.read()

f2.write(content)

此方法一次性將原始檔儲存的所有資料載入到記憶體中,記憶體外洩風險極大,一旦發生記憶體外洩,需要開闢硬碟作為虛擬記憶體,嚴重拖慢計算機的執行速度。

2)第二種方法

src_file = input('請輸入原始檔路徑:').strip()

dst_file = input('請輸入目標檔案路徑:').strip()

with open(r'{}'.format(src_file),'rb') as f1,\

open(r'{}'.format(dst_file),'wb') as f2:

for line in f1:

f2.write(line)

此方法將原始檔內容一行行載入到記憶體中複製給另外乙個檔案,如果原始檔每一行的內容適量,則相安無事;但不排除有的行會出現非常非常多的內容,那樣同樣對記憶體造成威脅。

3)第三種方法

src_file = input('請輸入原始檔路徑:').strip()

dst_file = input('請輸入目標檔案路徑:').strip()

with open(r'{}'.format(src_file),'rb') as f1,\

open(r'{}'.format(dst_file),'wb') as f2:

while true:

content = f1.read(1024) #指定每次迴圈讀入記憶體的內容為1024bytes

if len(content) == 0:

break

f2.write(content)

此方法指定每次讀入記憶體的資料為1024位元組,不會對記憶體造成威脅。

1)r+模式

with open(r'a.txt','r+b') as f:

print(f.tell()) #指標位於開始位置,列印0

f.seek(7,1) #指標向後移動7,指標位於7的位置

content = f.read().decode('utf-8') #讀取指標7後面的內容並進行轉碼

print(f.tell()) #28 此時指標處於末尾

f.seek(-21,2) #指標相對於末尾向前移動21個位元組,回到位置7

print(f.tell()) #7

f.write('他好我也好'.encode('utf-8')) #從當前指標位置7開始寫入'他好我也好',指標向後移動15個位元組

print(f.tell()) #22

f.seek(-15,1) #指標相對於當前位置22向前移動了15個位元組,回到位置7

print(f.tell()) #7

print(f.read().decode('utf-8')) #'他好我也好』覆蓋了'你好我好大',輸出'他好我也好家好'

print(f.tell()) #28

2)w+模式

with open(r'a.txt','w+b') as f:

print(f.tell()) #輸出0,清空檔案內容,指標位於檔案開頭。

print(f.tell()) #輸出28;指標位於檔案末尾

f.seek(-21,2) #指標移動到相對於檔案末尾為21的位置,即移動到檔案指標為7的位置

print(f.tell()) #輸出7

f.seek(-7,1) #指標相對於當前位置向前移動7個位元組,移動到檔案開頭,指標位置為0

print(f.tell()) #輸出0

print(f.read(10).decode('utf-8')) #讀入前10個位元組的內容,輸出: abcdefg你

print(f.tell()) #輸出10

3)a+模式

with open(r'a.txt','a+b') as f:

print(f.tell()) #指標位於檔案末尾,輸出為28

f.seek(-12,2) #指標移動到檔案末尾前12位元組處,位置為16,

print(f.tell()) #輸出16

f.seek(-9,1) #指標相當於當前位置向前移動9個位元組,位置為7

print(f.tell()) #輸出7

f.flush() #將寫入緩衝區的內容flush一下

print(f.tell()) #輸出52 不進行flushe則輸出31,只是指標位置顯示不對而已,實際上已經在末尾了

f.seek(-24,1) #相對於當前位置(末尾)向前移動24個位元組,指標位置為28

print(f.tell()) #輸出28

print(f.tell()) #輸出52

1)監測自己輸入的內容
with open(r'access.log','a+b') as f:

while true:

content = input('輸入一段內容:').strip()

f.write(content.encode('utf-8'))

num = len(content.encode('utf-8'))

f.seek(-num,2)

print(f.read().decode('utf-8'))

2)監測別的使用者訪問
import time

with open(r'access.log','rb') as f:

f.seek(0,2) #將指標移動到檔案末尾

while true:

content = f.readline()

if len(content) == 0:

time.sleep(0.5)

else:

print(content.decode('utf-8'),end='')

Python學習之檔案處理詳解

本文和大家分享的主要是python 學習python有所幫助。檔案處理 開啟檔案時,需要指定檔案路徑和以何等方式開啟檔案,開啟後,可以將結果賦值給乙個變數,這個變數我們稱為控制代碼。這樣我們就可以通過這個控制代碼對此檔案進行操作。使用後關閉。f open 檔案路徑 開啟方式 encoding 字元編...

Python學習筆記之檔案處理

1 簡單檔案操作流程 開啟檔案 操作 寫入 讀取等 關閉檔案 2 開啟檔案 open fname,mode,encoding,buf 檔案路徑 檔案,開啟方式,開啟檔案編碼格式,緩衝 buffering 大小 可選引數 r 唯讀 w 只寫,每次寫入都會把之前的內容覆蓋 a 追加,在之前的內容後面追加...

python高階之異常處理

異常處理 在 執行時,會因為各種原因出現bug,而程式遇到bug就會中斷執行,而在日常生產中程式是要長時間執行不能隨意中斷的。因此就需要我們提前做好異常處理。異常print x 一般報錯就會列印一串紅色的錯誤資訊 異常處理 為了更合理的處理 可能出現的錯誤 try print x except na...