操作檔案的方法

2022-08-23 20:36:15 字數 3945 閱讀 4364

1、重點

讀檔案

f.read() #

讀取所有內容,執行完該操作後,檔案指標會移動到檔案末尾

f.readline()

#讀取一行內容,游標移動到第二行首部

f.readlines()

#讀取每一行內容,存放於列表中

f.read()與f.readlines()

#都是將內容一次性讀入內容,如果內容過大會導致記憶體溢位,若還想將內容全讀入記憶體,則必須分多次讀入,有兩種實現方式:

#方式一

with open('

a.txt

',mode='

rt',encoding='

utf-8

') as f:

for line in

f:

print(line) #

同一時刻唯讀入一行內容到記憶體中

#方式二

with open('

1.mp4

',mode='rb'

) as f:

while

true:

data=f.read(1024) #

同一時刻唯讀入1024個bytes到記憶體中

if len(data) ==0:

break

print(data)

寫操作

f.write('

1111\n222\n

') #

針對文字模式的寫,需要自己寫換行符

f.write('

1111\n222\n

'.encode('

utf-8

')) #

針對b模式的寫,需要自己寫換行符

f.writelines(['

333\n

','444\n

']) #

檔案模式

f.writelines([bytes('

333\n

',encoding='

utf-8

'),'

444\n

'.encode('

utf-8

')]) #

b模式

2、主動控制檔案內指標移動

with open('

林允兒.txt

','r

',encoding='

utf8

')as f:

print(f.read(6)) #

'r'預設為rt,r讀取6個字元

with open('

林允兒.txt

','rb

')as f:

print(f.read(3)) #

rb讀取3個bytes

f.seek(offset,whence)

offset: 相對偏移度 (游標移動的位數)針對的是位元組

whence:指定游標位置從何開始

0:從檔案開頭

1:從當前位置

2:從檔案末尾

補充:utf-8:

中文是3個bytes

英文是1個bytes

gbk:

中文是2個bytes

英文是1個bytes

open函式不設定encoding,預設是gbk

與encode一毛錢都沒有,encoding只是乙個引數

除了read裡面的引數是針對字元,其他都是針對位元組

with open('

林允兒.txt

','rb

')as f: #

預設開啟檔案的編碼是gbk

f.seek(3,0) #

游標指在3個位元組後面, 因為是rb,如果是rt就是指在3個字元後面

print(f.tell()) #

列印出游標指在的位置

print(f.read(2))

解碼:

with open('

林允兒.txt

',mode='rb'

) as f:

f.seek(3,0)

print

(f.tell())

print(f.read(3).decode('

utf8

')) #

讀了3個位元組,解碼成utf8後就是乙個中文字的位元組

編碼:

with open('

林允兒.txt

','r

') as f:

f.seek(3,0)

print

(f.tell())

print(f.read(2).encode('

gbk')) #

r是讀字元,,所以讀了2位字元後編碼成gbk,所以就是4個位元組

1模式:

with open('

林允兒.txt

','rb

')as f:

f.seek(3,1) #

1就是當前游標位置

print

(f.tell())

print(f.read(3).decode('

utf8

')) #

文件怎麼編碼就用什麼解碼

2模式:

with open('

林允兒.txt

',mode='rb'

) as f:

f.seek(-6,2) #

2是游標位置在末尾,-x:往前x位元組

print(f.tell()) #

游標目前所在的位置,從前往後數

print(f.read(5).decode('

utf8

')) #

read是往後讀

寫入檔案:

import

time

res = time.strftime('

%y-%m-%d %h:%m:%s')

with open(

'林允兒.txt

','a

',encoding='

utf-8

')as f:

for i in range(5):

f.write(f

':sean老師手下留情\n

')

修改檔案內容:

方案一:

with open('

林允兒.txt

','r

',encoding='

gbk'

) as f:

data =f.read()

print

(data)

print

(type(data))

with open(

'林允兒.txt

','w

',encoding='

gbk'

) as f:

res = data.replace('

查楚文','大大'

) f.write(res)

方案二:

import os  #

os模組 在修改檔案和重新命名等操作時都要加

with open(

'林允兒.txt

','r

')as rf,\

open(

'作業2.txt

','w

')as wf:

data =rf.read()

res = data.replace('

大大', '

查楚文'

) wf.write(res)

os.remove(

'林允兒.txt')

os.rename(

'作業2.txt

','b.txt

')

獲取實時動態:

操作檔案常用的方法

檔案轉byte 絕對路徑 byte public static byte filetobytes string path 檔案轉base64 絕對路徑 base64 public static string filetobase64 string path byte轉base64 byte 轉bas...

iOS 操作檔案目錄的方法

使用目錄的常用方法 獲取當前目錄 nsstring currentdirectorypath 更改當前目錄 bool changecurrentdirectorypath nsstring path 複製目錄或檔案 bool copyitematpath nsstring srcpath topat...

iOS 操作檔案目錄的方法

使用目錄的常用方法 cpp view plain copy 獲取當前目錄 nsstring currentdirectorypath 更改當前目錄 bool changecurrentdirectorypath nsstring path 複製目錄或檔案 bool copyitematpath ns...