使用open(函數)來打開一個文件,獲取到文件句柄(文件的標識,每一個文件都有本身獨一無二的句柄)函數
open(文件名(或路徑),mode='打開方式',encoding='字符集(如gbk)')動畫
打開文件的方式:r,w,a,r+,w+,a+,rb,wb,ab,r+b,w+b,a+b.ui
讀取完文件句柄必定要關閉 f.close()編碼
1.spa
1 f=open('國家隊',mode='r',encoding='utf-8') 2 s=f.read() 3 print(s) 4 f.close() 5 # 張繼科景甜
6 # 賈乃亮
7 # 馬龍
8 # 郭晶晶
9 # 田亮
10 # 葉一茜
11 # 倪妮
這裏的encoding表示編碼集,對大部分的人而言更多的是utf-8,與encode無關.code
打開方式是rb的時候,後面不能寫encoding=''.blog
1 f=open('e:/153.jpg',mode='rb') 2 s=f.read() 3 print(s) 4 f.close() 5 # b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x01\x01,\x01,\x00\x00\xff\xe1\x00\xd4Exif\x00\x00MM\
rb讀取的是非文本文件,是沒法顯示出來的,顯示出來的是bytes類型.ip
2.絕對路徑和相對路徑內存
絕對路徑:從磁盤根目錄開始一直到文件名.utf-8
相對路徑:若是在同一個文件夾中,相對路徑就是這個文件名.若是在上一層文件夾,要用../
推薦使用相對路徑.由於若是把程序拷貝給別人使用,絕對路徑會有不少麻煩
3.讀取文件的方法
3.1 read()是將文件中的內容所有讀取出來,可是在實際操做中,儘可能不要用,由於若是文件過大,容易致使內存崩潰.
read(n)是讀取n個字符,若是是rb模式,讀取出來的是n個字節.若是再次讀取,會從當前位置讀,而不是從頭讀.
1 f=open('e:/153.jpg',mode='rb') 2 s=f.read(1) 3 print(s) #b'\xff'
4 a=f.read(1) #b'\xd8'
5 print(a) 6 f.close() 7
8
9 f=open('國家隊',mode='r',encoding='utf-8') 10 s=f.read(1) 11 print(s) #張
12 a=f.read(1) 13 print(a) #繼
14 f.close()
3.2 readline() 一次讀取一行的數據.\n也會讀出來,若是想去掉用strip()方法,同時readline()自帶一個\n.
1 f=open('../day07/國家隊',mode='r',encoding='utf-8') 2 content=f.readline().strip() 3 content1=f.readline().strip() 4 content2=f.readline().strip() 5 content3=f.readline().strip() 6 print(content) 7 print(content1) 8 print(content2) 9 print(content3)
張繼科景甜
賈乃亮
馬龍
郭晶晶
3.3 leadlines() 每一行做爲一個字符串元素放到列表中,會將全部的內容讀取出來,因此也容易出現內存奔潰的問題.
1 f=open('../day07/國家隊',mode='r',encoding='utf-8') 2 lis=f.readlines() 3 print(lis) #['張繼科景甜\n', '賈乃亮\n', '馬龍\n', '郭晶晶\n', '田亮\n', '葉一茜\n', '倪妮']
3.4 循環讀取.每次讀取一行內容
1 f=open('../day07/國家隊',mode='r',encoding='utf-8') 2 for line in f: 3 print(line.strip())
寫入的時候,若是沒有該文件,則會建立文件:
若是這個文件存在,則將原文件中的內容刪除,在寫入新內容(屢次使用f.write()只有第一次將原文件中的內容所有刪除)
1 f=open('動畫片.txt',mode='w',encoding='utf-8') 2 f.write('金毛獅王') 3 f.write('大耳朵圖圖') 4 f.write('小耳朵我') #金毛獅王大耳朵圖圖小耳朵我
5 f.flush() 6 f.close()
wb的模式下,寫文件時,必須將字符串轉換成utf-8的byte()數據.
1 f=open('動畫片.txt',mode='wb') 2 f.write('他大舅'.encode('utf-8')) 3 f.flush() 4 f.close()
寫入的內容會追加到文件的結尾
1 f=open('動畫片.txt',mode='a') 2 f.write('qwert') #qwertqwert
3 f.flush() 4 f.close()
對於讀寫模式,必須是先讀,而後再進行寫入.不然會將開頭的內容覆蓋掉.,這裏的write是追加,不會刪除原文件.
1 f=open('動畫片.txt',mode='r+',encoding='utf-8') 2 a=f.read() 3 print(a) #qwertqwertqwertyuiqwertyui是的法規
4 b=f.write('qwertyui是的法規') #這裏的write是在原文件後追加,不是刪除後再添加
5 f.flush() 6 f.close()
現將全部的內容清空,而後寫入,最後讀取,但讀取的內容是空的.
1 f=open('動畫片.txt',mode='w+',encoding='utf-8') 2 f.write('haha') #haha
3 content=f.read() #空
4 print(content) 5 f.flush() 6 f.close()
1 f=open('動畫片.txt',mode='a+',encoding='utf-8') 2 f.write('劉的愛') #qwertykl劉的愛
3 content=f.read() #空
4 print(content) 5 f.flush() 6 f.close()
1. seek(n) 將光標移動到n位置,這裏n的單位是byte,若是使用的是utf-8的中文,要是三的倍數.
移動到開頭:seek(0)
移動到結尾:seek(0,2)
seek中的第二個參數表明從哪一個位置進行移動,0表示開頭,1表示當前位置,2表示結尾.第一個參數表明偏移量.
2. tell() 能夠幫助咱們獲取當前光標的位置(單位是byte)
3. f.truncate() 刪除光標後面的全部內容
★在r+模式下,光標無論用,若是讀取了內容,無論光標是多少.再寫入和操做的時候都是在結尾進行的
建立一個文件的副本,在副本中修改信息,修改完成後,將新文件的名字改爲老文件的名字.
1 import os #引入os模塊
2 with open('彩票.txt',mode='r',encoding='utf-8') as f1,open('彩票_new.txt',mode='w',encoding='utf-8') as f2: #另外一種打開方式
3 content=f1.read() 4 new_content=content.replace('疾病','健康') 5 f2.write(new_content) 6 os.remove('彩票.txt') #刪除'彩票.txt'
7 os.rename('彩票_new.txt','彩票.txt') #重命名
以上 一次將全部的內容都讀取了,內存容易奔潰,解決此問題的方案是能夠一行一行的讀取和操做:
1 import os 2 with open('彩票.txt',mode='r',encoding='utf-8') as f1,open('彩票_new.txt',mode='w',encoding='utf-8') as f2: 3 for line in f1: 4 new_line=line.replace('疾病','健康') 5 f2.write(new_line) 6 os.remove('彩票.txt') 7 os.rename('彩票_new.txt','彩票.txt')