1. 程式人生 > >3 文件操作

3 文件操作

test 增加 spl src replace .com pen AD info

一 文件操作

1 文件路徑

2 編碼方式:utf-8

3 動作mode:讀 ,讀寫,寫讀..

例子:

 1 # f1 = open(‘D:\a.txt‘, encoding=‘utf-8‘, mode=‘r‘)
 2 # content = f1.read()
 3 # print(content)
 4 # f1.close()
 5 
 6 
 7 f1,文件句柄,文件對象,file,f_handle,file_handle,f_obj
 8     open打開的指令,windows的指令,
 9     windows 默認編碼方式gbk,linux默認編碼方式utf-8,mac utf-8
10 11 12 1,打開文件,產生文件句柄。 13 2,操作文件句柄。 14 3,關閉文件。

二 文件的打開模式

 1 #1. 打開文件的模式有(默認為文本模式):
 2 r ,只讀模式【默認模式,文件必須存在,不存在則拋出異常】
 3 w,只寫模式【不可讀;不存在則創建;存在則清空內容】
 4 a, 只追加寫模式【不可讀;不存在則創建;存在則只追加內容】
 5 
 6 #2. 對於非文本文件,我們只能使用b模式,"b"表示以字節的方式操作(而所有文件也都是以字節的形式存儲的,使用這種模式無需考慮文本文件的字符編碼、圖片文件的jgp格式、視頻文件的avi格式)
 7 rb 
8 wb 9 ab 10 註:以b方式打開時,讀取到的內容是字節類型,寫入時也需要提供字節類型,不能指定編碼 11 12 #3,‘+’模式(就是增加了一個功能) 13 r+, 讀寫【可讀,可寫】 14 w+,寫讀【可寫,可讀】 15 a+, 寫讀【可寫,可讀】 16 17 #4,以bytes類型操作的讀寫,寫讀,寫讀模式 18 r+b, 讀寫【可讀,可寫】 19 w+b,寫讀【可寫,可讀】 20 a+b, 寫讀【可寫,可讀】

1 r+詳細解釋:讀寫

 1 # f1 = open(‘log1‘, encoding=‘utf-8‘, mode=‘r+‘)
 2 # print(f1.read())
3 # f1.write(‘666‘) 4 # f1.close() 5 6 7 # f1 = open(‘log1‘, encoding=‘utf-8‘, mode=‘r+‘) 8 # f1.seek(0,2) ##把光標調整到最後 9 # f1.write(‘6666‘) 10 # f1.seek(0)#調整光標 11 # print(f1.read()) 12 # #光標 按照字節去運轉 seek 13 # f1.close()

2 w只寫,默認會情況之前的內容

# f1 = open(‘log2‘, encoding=‘utf-8‘, mode=‘w‘)
# f1.write(‘abc是披著高富帥外衣的純屌絲.....‘)
# f1.close()

wb模式

# f1 = open(‘log2‘, mode=‘wb‘)
# f1.write(‘alex是披著高富帥外衣的純屌絲.....‘.encode(‘utf-8‘))
# f1.close()

w+寫讀模式

1 # f1 = open(‘log2‘, encoding=‘utf-8‘, mode=‘w+‘)
2 # print(f1.read())
3 # f1.write(‘666‘)
4 # f1.close()

a追加和a+(可寫,可讀)

 1 # f1 = open(‘log2‘, encoding=‘utf-8‘, mode=‘a‘)
 2 # f1.write(‘\n老男孩‘)
 3 # f1.close()
 4 
 5 #a+可寫可讀
 6 # f1 = open(‘log2‘, encoding=‘utf-8‘, mode=‘a+‘)
 7 # f1.write(‘fdsafdsafdsagfdg‘)
 8 # f1.seek(0)
 9 # print(f1.read())
10 # f1.close()

三 文件的改

#1,打開原文件,產生文件句柄。
#2,創建新文件,產生文件句柄。
#3,讀取原文件,進行修改,寫入新文件。
#4,將原文件刪除。
#5,新文件重命名原文件。

1 import os
2 with open(file_test, encoding=utf-8) as f1,3     open(file_test.bak, encoding=utf-8, mode=w) as f2:
4     for line in f1:
5         new_line = line.replace(SB,alex)
6         f2.write(new_line)
7 os.remove(file_test)
8 os.rename(file_test.bak,file_test)

四 文件的其他操作

#read read(n) readline() readlines() write() close readable writable

技術分享圖片
 1 # read 全部讀出
 2 # f1 = open(‘log1‘, encoding=‘utf-8‘)
 3 # content = f1.read()  #
 4 # print(content)
 5 # f1.close()
 6 
 7 
 8 #read(n)
 9 # f1 = open(‘log1‘, encoding=‘utf-8‘)
10 # content = f1.read(5)  # r 模式 按照字符讀取。
11 # print(content)
12 # f1.close()
13 
14 # f1 = open(‘log1‘, mode=‘rb‘)
15 # content = f1.read(3)  # rb模式 按照字節讀取。
16 # print(content.decode(‘utf-8‘))
17 # f1.close()
18 
19 #readline()按行讀取 讀幾行,下面寫幾行
20 # f1 = open(‘log1‘, encoding=‘utf-8‘)
21 # print(f1.readline())
22 # print(f1.readline())
23 # print(f1.readline())
24 # print(f1.readline())
25 # f1.close()
26 
27 
28 #readlines() 將每一行作為列表的一個元素並返回這個列表
29 # f1 = open(‘log1‘, encoding=‘utf-8‘)
30 # print(f1.readlines())
31 # f1.close()
32 
33 
34 #for循環
35 # f1 = open(‘log1‘, encoding=‘utf-8‘)
36 # for i in f1:
37 #     print(i)
38 # f1.close()
文件讀取方式

tell指針

1 #tell 告訴指針的位置
2 # f1 = open(‘log2‘, encoding=‘utf-8‘, mode=‘w‘)
3 # f1.write(‘fdsafdsafdsagfdg‘)
4 # print(f1.tell())
5 # f1.close()
6 #seek(參數),seek(0,2) 調至最後 按照字節去調整光標

五 編碼轉換

1 # s1 = b‘\xd6\xd0\xb9\xfa‘
2 # s2 = s1.decode(‘gbk‘)
3 # s3 = s2.encode(‘utf-8‘)
4 # print(s3)  # b‘\xe4\xb8\xad\xe5\x9b\xbd‘
5 # s1 = b‘\xd6\xd0\xb9\xfa‘.decode(‘gbk‘).encode(‘utf-8‘)
6 # print(s1)

技術分享圖片

3 文件操作