Python檔案操作方法詳解
阿新 • • 發佈:2020-02-10
本節內容
1、檔案常用操作彙總
2、開啟檔案
3、操作檔案
4、關閉檔案
一、檔案常用操作彙總
二、開啟檔案
1、普通開啟模式
- r,英文:read,只讀模式(預設)
- w,英文:write,只寫模式(不可讀,不存在則建立新檔案,存在則刪除內容)
- a,英文:append,追加模式(不可讀,不存在則建立,存在則只追加內容
2、同時讀寫模式
- r+,可讀寫檔案(可讀;可寫;可追加,不存在檔案則報錯)
- w+,可寫讀檔案(可讀,可寫,建立新檔案)
- a+,可追加和讀檔案(可讀,可追加,不存在則建立)
3、二進位制開啟模式
- rb,二進位制讀
- wb,二進位制寫
- ab,二進位制追加
三、操作檔案
檔案內容:
Somehow,it seems the love I knew was always the most destructive kind 不知為何,我經歷的愛情總是最具毀滅性的的那種 Yesterday when I was young 昨日當我年少輕狂
1、read()
當read()函式中傳入整數(int)引數,則讀取相應的字元數,如果不填寫,則預設讀取所有字元
f = open("yesterday2",'r',encoding="utf-8") #預設讀取全部字元 print(f.read()) f.close() #輸出 Somehow,it seems the love I knew was always the most destructive kind 不知為何,我經歷的愛情總是最具毀滅性的的那種 Yesterday when I was young 昨日當我年少輕狂 f = open("yesterday2",encoding="utf-8") #只讀取10個字元 print(f.read(10)) f.close() #輸出 Somehow,i
注:只有當檔案有讀許可權時,才可以操作這個函式
2、獲取檔案控制代碼所在的指標的位置tell()
獲取檔案控制代碼所在的指標的位置
f = open("yesterday2",encoding="utf-8") print(f.read(10)) #獲取指標位置 print(f.tell()) f.close() #輸出 Somehow,i #讀取的內容 10 #指標位置
3、設定檔案控制代碼所在的指標位置seek()
f = open("yesterday2",encoding="utf-8") print(f.read(10)) #設定之前的指標位置 print(f.tell()) f.seek(0) #設定之後的指標位置 print(f.tell()) f.close() #輸出 Somehow,i #讀取檔案的內容 10 #設定之前的指標位置 0 #設定之後的指標位置
4、列印檔案的編碼encoding
f = open("yesterday2",encoding="utf-8") print(f.encoding) f.close() #輸出 utf-8
5、fileno()
返回檔案控制代碼在記憶體中的編號
f = open("yesterday2",encoding="utf-8") print(f.fileno()) f.close() #輸出 3
6、name
返回檔名
f = open("yesterday2",encoding="utf-8") print(f.name) f.close() #輸出 yesterday2
7、isatty()
判斷是否是一個終端裝置(比如:印表機之類的)
f = open("yesterday2",encoding="utf-8") print(f.isatty()) f.close() #輸出 False #表示不是一個終端裝置
8、seekable()
不是所有的檔案都可以移動游標,比如tty檔案,可以移動的,返回True
f = open("yesterday2",encoding="utf-8") print(f.seekable()) f.close() #輸出 True
9、readable()
檔案是否可讀
f = open("yesterday2",encoding="utf-8") print(f.readable()) f.close() #輸出 True
10、writeable()
檔案是否可寫
f = open("yesterday2",encoding="utf-8") print(f.writable()) f.close() #輸出 False #檔案不可寫
11、flush()
寫資料時,寫的資料不想存記憶體中快取中,而是直接存到磁碟上,需要強制重新整理
>>> f = open("yesterday2","w",encoding="utf-8") #這時'hello word'在快取中 >>> f.write("hello word") #強刷到磁碟上 >>> f.flush()
這個怎麼實驗呢?在cmd命令列中,cd到你檔案所在的路徑下,然後輸入python,在Python命令列中輸入上面程式碼
①cd d:\PycharmProjects\pyhomework\day3下(因為我的被測檔案在這個資料夾下)
②在這個目錄下輸入Python命令列,然後進行測試
③強制重新整理之前
④執行強刷命令之後
⑤強刷後文件中的內容變化
注:以寫的模式開啟檔案,寫完一行,預設它是寫到硬碟上去的,但是其實它不一定寫到硬碟上去了。當你剛寫完一行,如果此時斷電,有可能,你這行就沒有寫進去,因為這一樣還在記憶體的快取中(記憶體中的快取機制),所以你有不想存快取,所以就要強制重新整理。那一般在什麼情況下用吶?比如:存錢
12、closed
判斷檔案是否關閉
f = open("yesterday2","r",encoding="utf-8") f.read() print(f.closed) #輸出 False
13、truncate(擷取字元的數)
擷取檔案中的字串,開啟檔案模式一定是追加模式(a),不能是寫(w)和讀(r)模式
#沒有指標 f = open("yesterday2","a",encoding="utf-8") f.truncate(10) f.close() #擷取結果 Somehow,i #有指標 f = open("yesterday2",encoding="utf-8") f.seek(5) f.truncate(10) f.close() #擷取結果 Somehow,i
說明truncate擷取檔案中的欄位,並不受指標(seek)所在位置影響。
14、write()
寫入檔案內容
f = open("yesterday2",encoding="utf-8") f.write("Somehow,it seems the love I knew was always the most destructive kind") f.close()
注:寫功能只有當開啟檔案模式是寫(w)或者追加(a)才可操作。
四、關閉檔案
f.close()
更多關於Python檔案操作方法請檢視下面的相關文章