1.直接打開文件並賦值給變量,打開後獲得操做句柄,但不會自動關閉java
fd = open('../config/file1.txt','r',encoding='utf-8')
2.使用with子句,打開後文件會自動關閉,建議使用,並能夠同時打開多個文件linux
with open('../config/file1.txt','r',encoding='utf-8') as fd1,\ open('../config/file2.txt','r',encoding='utf-8') as fd2: print("I had open two files")
========= =============================================================== Character Meaning --------- --------------------------------------------------------------- 'r' open for reading (default) 'w' open for writing, truncating the file first 'x' create a new file and open it for writing 'a' open for writing, appending to the end of the file if it exists 'b' binary mode 't' text mode (default) '+' open a disk file for updating (reading and writing) 'U' universal newline mode (deprecated) ========= ===============================================================
1.’r',默認模式,參數能夠不寫,打開只讀文件,寫入報錯windows
>>> fd = open('../config/file1.txt','r',encoding='utf-8') >>> fd.write('java c rubby') Traceback (most recent call last): File "<stdin>", line 1, in <module> io.UnsupportedOperation: not writable
2.‘w’,先truncate原文件,後寫入,不可讀,文件不存在則建立緩存
>>> fd = open('../config/file1.txt','w',encoding='utf-8') >>> print(fd.read()) Traceback (most recent call last): File "<stdin>", line 1, in <module> io.UnsupportedOperation: not readable >>> fd.write('java rubby go') 13 >>> fd.close() >>> fd = open('../config/file1.txt','r',encoding='utf-8') >>> fd.read() 'java rubby go'
3.'x',建立新文件,打開並寫入,若是文件已經存在,則報錯app
>>> fd = open('../config/file21.txt','x',encoding='utf-8' ) >>> fd.read() Traceback (most recent call last): File "<stdin>", line 1, in <module> io.UnsupportedOperation: not readable >>> fd.write('123456') 6 >>> fd.close() >>> fd = open('../config/file21.txt','r',encoding='utf-8') >>> fd.read() '123456' >>> fd.close() >>> fd = open('../config/file21.txt','x',encoding='utf-8') Traceback (most recent call last): File "<stdin>", line 1, in <module> FileExistsError: [Errno 17] File exists: '../config/file21.txt'
4.’a',追加寫內容到文件末尾dom
>>> fd = open('../config/file1.txt','a',encoding='utf-8') >>> fd.write('linux windows aix') 17 >>> fd.close() >>> fd = open('../config/file1.txt','r',encoding='utf-8') >>> fd.read() 'java rubby golinux windows aix'
5.'b',二進制模式,好比流文件mp3,而且須要同時指定一種讀寫模式ui
>>> fd = open('/tmp/Front_Right.wav','b') Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: Must have exactly one of create/read/write/append mode and at most one plus >>> fd = open('/tmp/Front_Right.wav','rb') >>> fd1 = open('/tmp/Front_Right.wav','wb') >>> fd2 = open('/tmp/Front_Right.wav','ab') >>> fd2 = open('/tmp/Front_Right.wav','w+b') >>> fd2 = open('/tmp/Front_Right.wav','r+b')
6.'t',文本模式,默認打開文本並讀取模式rtthis
7.'+',打開硬盤文件讀寫編碼
>>> fd = open('../config/file1.txt','r+',encoding='utf-8') >>> fd.read() 'java rubby golinux windows aix' >>> fd.write("mage") 4 >>> fd.seek(0) 0 >>> fd.read() 'java rubby golinux windows aixmage'
>>> fd = open('../config/file4.txt','w+',encoding='utf-8') >>> fd.write('guangzhou') 9 >>> fd.seek(0) 0 >>> fd.read() 'guangzhou' >>> fd.seek(0) 0 >>> fd.write('hangzhou') 8 >>> fd.seek(0) 0 >>> fd.read() 'hangzhouu'
>>> fd = open('../config/file4.txt','a+',encoding='utf-8') >>> fd.read() '' >>> fd.seek(0) 0 >>> fd.read() 'hangzhouu' >>> fd.close() >>> fd = open('../config/file4.txt','a+',encoding='utf-8') >>> fd.write('beijing') 7 >>> fd.read() '' >>> fd.seek(0) 0 >>> fd.read() 'hangzhouubeijing'
8.‘U’,deprecatedspa
1.f.tell(),告知字符指針位置
2.f.seek(),移動字符指針位置,f.seek(0)文件開頭
>>> fd = open('../config/file4.txt','r',encoding='utf-8') >>> fd.tell() 0 >>> fd.seek(0) 0 >>> fd.tell() 0 >>> fd.read(1) 'h' >>> fd.tell() 1 >>> fd.read(2) 'an' >>> fd.tell() 3 >>> fd.seek(0) 0 >>> fd.readline() 'hangzhouubeijing\n' >>> fd.tell() 17
1.fd.read(size)
>>> fd = open('../config/file4.txt','r',encoding='utf-8') >>> fd.read() 'hangzhouubeijing' >>> fd.seek(0) 0 >>> fd.read(1) 'h' >>> fd.read(2) 'an' >>> fd.read(3) 'gzh' >>> fd.seek(0) 0 >>> fd.read(6) 'hangzh'
2.fd.readline(size)
>>> fd = open('../config/file4.txt','r',encoding='utf-8') >>> fd.readline() 'hangzhouubeijing\n' >>> fd.readline() 'shenzhen\n' >>> fd.readline() 'shanghai\n' >>> fd.readline(1) 'a' >>> fd.readline(2) 'nh' >>> fd.readline() 'ui\n' >>> fd.readline() 'guangdong\n' >>> fd.readline() 'zhejiang' >>> fd.readline() ''
3.fd.readlines(size)
>>> fd = open('../config/file4.txt','r',encoding='utf-8') >>> fd.readlines() ['hangzhouubeijing\n', 'shenzhen\n', 'shanghai\n', 'anhui\n', 'guangdong\n', 'zhejiang'] >>> fd.seek(0) 0 >>> fd.readlines(1) ['hangzhouubeijing\n'] >>> fd.readlines() ['shenzhen\n', 'shanghai\n', 'anhui\n', 'guangdong\n', 'zhejiang']
4.fd.readable()
>>> fd = open('../config/file4.txt','r',encoding='utf-8') >>> fd.readable() True
>>> fd = open('../config/file4.txt','r',encoding='utf-8') >>> for line in fd: ... print(line) ... hangzhouubeijing shenzhen shanghai anhui guangdong zhejiang >>> fd.seek(0) 0 >>> for index,line in enumerate(fd.readlines()): ... print(index,line) ... 0 hangzhouubeijing 1 shenzhen 2 shanghai 3 anhui 4 guangdong 5 zhejiang >>>
close(self, /) 關閉打開的文件
detach(self, /) 幹嗎用?
>>> fd.detach() <_io.BufferedReader name='../config/file4.txt'> >>> fd.read() Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: underlying buffer has been detached
fileno(self, /) 返回文件描述符,幹嗎用?
>>> fd = open('../config/file4.txt','r',encoding='utf-8') >>> fd.fileno() 4 >>> fd = open('../config/filexxx.txt','w+',encoding='utf-8') >>> fd.fileno() 3
flush(self, /) 將緩存當即寫入硬盤,提升效率
import time import sys for i in range(40): sys.stdout.write("#") sys.stdout.flush() time.sleep(0.1)
isatty(self, /) 是否鏈接到終端設備
seekable(self, /)
truncate(self, pos=None, /)
>>> fd = open('../config/file4.txt','r+',encoding='utf-8') >>> fd.truncate() 0
writable(self, /) 判斷文件是否以寫模式打開
| Return whether object was opened for writing.
>>> fd = open('../config/file4.txt','r+',encoding='utf-8') >>> fd.writable() True >>> fd = open('../config/file1.txt','r',encoding='utf-8') >>> fd.writable() False
1.所有讀入內存,修改完畢以後覆蓋寫入源文件
2.一行一行讀取內存,修改完畢以後寫入新文件,用新文件覆蓋舊文件