w = open('1.txt
', '
w', encoding='
utf-8
') #
w:沒有檔案新建檔案,有檔案就清空檔案
w.write('
000\n')
w.flush()
#在寫入大量資料,要及時處理記憶體空間,不然記憶體可能溢位導致資料丟失
w.write('
111\n')
w.flush()
#最後一次flush()操作可以省略
w.writelines(['
222\n
', '
333\n
']) #
一次性寫入多行
w.write('
444'
)w.write(
'555')
w.close()
#1.將記憶體的的資料重新整理到硬碟中;2.釋放檔案資源
#需求:#
完成文字型別的檔案複製:1.txt => 11.txt
r = open('
1.txt
', '
r', encoding='
utf-8')
w = open('
11.txt
', '
w', encoding='
utf-8')
for line in r: #
遍歷就是一行一行讀 取讀檔案的流
w.write(line)
w.flush()
w.close()
r.close()
①主模式:
r 讀 r+:不會建立檔案的可讀可寫w 寫 w+:建立清空檔案的可讀可寫
a 追加 a+:建立不清空檔案的可讀可寫
②從模式:
t 文字操作(預設模式),要規定文字的編碼格式utf-8b 飛文字檔案必須採用二進位制模式處理,不需要規定文字的編碼格式。+可讀可寫
x 寫,必須有自己的檔案,否則會報錯
③a為寫模式,在原資料後追加寫入新資料(追加寫入)
#temp.txt
#111222333
with open(
'temp.txt
', '
a', encoding='
utf-8
') as f:
f.write(
'new msg')
#結果為:111222333new msg
①文字型別檔案的複製
rw模式:r = open('
1.txt
', '
r', encoding='
utf-8')
w = open('
11.txt
', '
w', encoding='
utf-8')
for line in r: #
遍歷就是一行一行讀 取讀檔案的流
w.write(line)
w.flush()
w.close()
r.close()
with open('souurce.txt
','r
',encoding='
utf-8
') as f1:
with open(
'target.txt
','a+
',encoding='
utf-8
') as f2:
for line in
f1: f2.write(line)
案例:複製source.mp4為target.mp4with open(
'source.mp4
', 'rb'
) as f1:
with open(
'target.mp4
', 'wb'
) as f2:
for line in
f1: f2.write(line)
必須在b模式下操作,seek中偏移的是位元組
偏移量:移動的位元組數偏移位置:
1——從當前游標位置開始偏移
0——從檔案開始位置開始偏移
2——從檔案末尾開始偏移
①游標讀with open(
'source.txt
', 'rb'
) as f:
d1 = f.read(11)
(d1)
print(d1.decode('
utf-8'))
#當前游標的位置
(f.tell())
#游標操作 - 從末尾位置開始
f.seek(-3, 2)
d2 =f.read()
print(d2.decode('
utf-8
')) #
890#
游標操作 - 從當前位置開始
f.seek(-3, 1)
d2 =f.read()
print(d2.decode('
utf-8
')) #
34567890
#游標操作 - 從頭開始
f.seek(3, 0)
d2 =f.read()
(d2)
print(d2.decode('
utf-8
')) #
好1234567890
②游標寫:會覆蓋書寫
with open(
'source.txt
', '
rb+'
) as f:
f.seek(11)
(f.read())
f.write(b
'000
')
#案例with open('
001.png
', 'rb'
) as f:
data =f.read()
(len(data))
#在大檔案中,開頭| 1/3 | 2/3 | 末尾 各取10個位元組拼接成秒傳的資訊依據
#形成秒傳規則
tagdata = b''
with open(
'001.png
', 'rb'
) as f:
#通過其他途徑(sys模組)來獲取檔案總大小
data =f.read()
length =len(data)
#開頭f.seek(0, 0)
d1 = f.read(10)
#1/3
f.seek(length // 3, 0)
d2 = f.read(10)
#2/3
f.seek(length // 3 * 2, 0)
d3 = f.read(10)
#末尾f.seek(-10, 2)
d4 = f.read(10)
tagdata = d1 + d2 + d3 +d4
#秒傳依據
(tagdata)
newdata = b""
with open(
'001.png
', 'rb'
) as f:
data =f.read()
length =len(data)
f.seek(0, 0)
newdata += f.read(10)
f.seek(length // 3, 0)
newdata += f.read(10)
f.seek(length // 3 * 2, 0)
newdata += f.read(10)
f.seek(-10, 2)
newdata += f.read(10)
if newdata ==tagdata:
print('
秒傳成功')
else
:
print('
慢慢傳去吧
')
檔案操作模式詳解
一 with的使用 with open a.txt mode rt as f1 res f1.read print res f1 open a.txt mode rt with方法可以在執行完子 後自動關閉檔案,節約記憶體空間。二 指定字元編碼 強調 t和b不能單獨使用,必須跟r w a連用 t文字...
檔案操作模式擴充套件 游標操作
with open 檔案 模式 encoding utf 8 as f 操作 pass temp.txt 111222333 with open temp.txt a encoding utf 8 as f f.write new msg 結果 111222333new msg 文字型別檔案的複製 ...
檔案 游標操作模式詳解
w 沒有檔案新建檔案,有檔案就清空檔案 w open 1.txt w encoding utf 8 w.write 000 n w.flush 在寫入大量資料,要及時處理記憶體空間,不然記憶體可能溢位導致資料丟失 w.write 111 n w.flush 最後一次flush 操作可以省略 w.wr...