文件操做app
讀取: r (read):只能讀不能寫,文件不存在就報錯ui
#打開文件:
object = open('某txt文件',mode = 'r',encoding = '編碼')
#讀取文件全部內容到內存:
content = object.read()
print(content)
#讀取文件的全部內容到內存,並按照每一行進行分割到列表中。
content = object.readlines()
print(content)
#若是之後讀取一個特別大的文件
for line in object: #一行一行進行讀取
line = object.strip() #去除換行
print(line)
#關閉內容:
object.close()
read(): 所有讀到內存編碼
read(1)spa
1表示一個字符code
obj = open('某個txt文件',mode='r',encoding='utf-8')
data = obj.read(1) # 1個字符
obj.close()
print(data)
1表示一個字節(二進制中)blog
obj = open('某個txt文件',mode='rb') # b:二進制的意思
data = obj.read(3) # 1個字節
obj.close()
寫入:w (write): 只能寫不能讀(先清空文件),文件不存在就新建ip
#打開文件:
object = open('某個txt文件或要新建的文件',mode = 'w',encoding = '編碼')
#寫內容:
object.write('xxx')
#關閉文件:
object.close()
write(字符串)內存
obj = open('某個txt文件或要新建的文件',mode='w',encoding='utf-8')
obj.write('你好')
obj.close()
write(二進制)utf-8
obj = open('某個txt文件或要新建的文件',mode='wb')
# obj.write('你好'.encode('utf-8'))
v = '你好'.encode('utf-8')
obj.write(v)
obj.close()
追加; a (append): 只能追加不能讀,不存在則新建字符串
#打開文件:
object = open ('某個txt文件或要新建的文件',mode = 'a',encoding = '編碼')
#寫內容:
object.append()
#關閉文件
object.close()
seek(光標字節位置),不管模式是否帶b,都是按照字節進行處理。
obj = open('某個txt文件',mode='r',encoding='utf-8')
obj.seek(3) # 跳轉到指定字節位置,讀取後面內容
data = obj.read()
obj.close()
print(data)
obj = open('a.txt',mode='rb')
obj.seek(3) # 跳轉到指定字節位置
data = obj.read()
obj.close()
print(data)
tell(), 獲取光標當前所在的字節位置
obj = open('a.txt',mode='rb')
obj.read()
data = obj.tell()
print(data)
obj.close()
flush,強制將內存中的數據寫入到硬盤
v = open('a.txt',mode='a',encoding='utf-8')
while True:
val = input('請輸入:')
v.write(val)
v.flush()
v.close()
v = open('a.txt',mode='a',encoding='utf-8')
v.close()
with open('a.txt',mode='a',encoding='utf-8') as v:
data = v.read()
# 縮進中的代碼執行完畢後,自動關閉文件
with open('a.txt',mode='r',encoding='utf-8') as f1:
data = f1.read()
new_data = data.replace('飛灑','666')
with open('a.txt',mode='w',encoding='utf-8') as f1:
data = f1.write(new_data)
大文件修改
f1 = open('a.txt',mode='r',encoding='utf-8')
f2 = open('b.txt',mode='w',encoding='utf-8') #先打開兩個文件,再進行操做
for line in f1: # 一行一行進行修改
new_line = line.replace('要被修改的內容','被修改後的內容')
f2.write(new_line)
f1.close()
f2.close()
#另外一種寫法:
with open('a.txt',mode='r',encoding='utf-8') as f1, open('c.txt',mode='w',encoding='utf-8') as f2:
for line in f1:
new_line = line.replace('要被修改的內容','被修改後的內容')
f2.write(new_line)
三.練習:
1.請將user中的元素根據 _ 連接,並寫入 'a1.txt' 的文件
user = ['alex','eric'] data = '_'.join(user) object = open('a1.txt',mode = 'w',encoding = 'utf-8') object.write(data) object.close()
2.請將user中的元素根據 | 連接,並寫入 'a2.txt' 的文件
user = [ {'name':'alex','pwd':'123'}, {'name':'eric','pwd':'olbody'},] object = open('a2.txt',mode = 'w',encoding = 'utf-8') for item in user: line = '%s|%s\n' %(item['name'],item['pwd']) object.write(line) object.close()
3.請將a2.(根據2)txt中的文件讀取出來並添加到一個列表中 ['alex|123','eric|olbody']
方法一: list = [] objecct = open('a2.txt',mode = 'r',encoding = 'utf-8') for line in object: line = line.strip() list.append(line) print(list) 方法二: file_obj = open('a2.txt',mode='r',encoding='utf-8') content = file_obj.read() file_obj.close() content = content.strip() #去除最後一個換行 data_list = content.split('\n') #根據中間的換行進行切割 print(data_list)