[Python筆記] 文件指針
- 2019 年 12 月 5 日
- 筆記
說明
文件指針式指向當前位元組的位置,當mode=r時,指針起始在0;mode=a時,指針起始位置在EOF。
tell()
tell()顯示當前指針位置
In [47]: f = open('test') In [49]: f.tell() Out[49]: 0
seek()
seek()移動文件指針的位置,offest設置偏移多少個位元組,whence表示從哪裡開始偏移。
seek(offest[,whence])
文本模式下,whence取值範圍及釋義如下:
參數 |
值 |
釋義 |
---|---|---|
whence |
0 |
預設值,表示從頭開始,offest值只能是正整數 |
whence |
1 |
表示從當前位置開始,offest值只接受0 |
whence |
2 |
表示從EOF位置開始,offest值只接受0 |
# 文本模式 In [62]: f = open('test4','r+') In [63]: f.tell() Out[63]: 0 In [64]: f.read() Out[64]: 'addbsa 12323 \nwqeqweqwn' In [65]: f.tell() # 獲取當前文件指針位置 Out[65]: 24 In [67]: f.seek(0) # 設置文件指針為0,也就 從頭開始 Out[67]: 0 In [68]: f.read() # 從新read就是從頭開始讀取 Out[68]: 'addbsa 12323 \nwqeqweqwn' In [69]: f.seek(2,0) # 從EOF位置開始偏移2個字元位置 Out[69]: 2 In [70]: f.read() Out[70]: 'dbsa 12323 \nwqeqweqwn' In [71]: f.seek(2,1) # offest只接受0 --------------------------------------------------------------------------- UnsupportedOperation Traceback (most recent call last) <ipython-input-71-ad0d3a1f89a7> in <module> ----> 1 f.seek(2,1) UnsupportedOperation: can't do nonzero cur-relative seeks In [72]: f.seek(2,2) --------------------------------------------------------------------------- UnsupportedOperation Traceback (most recent call last) <ipython-input-72-c79d1a6e2c76> in <module> ----> 1 f.seek(2,2) UnsupportedOperation: can't do nonzero end-relative seeks In [73]: f.close() # 中文 In [74]: f = open('test4','w+') In [75]: f.write('連仕彤部落格') Out[75]: 5 In [76]: f.tell() Out[76]: 15 In [77]: f.close() In [78]: f = open('test4','r+') In [79]: f.read(3) Out[79]: '連仕彤' In [80]: f.seek(1) Out[80]: 1 In [81]: f.tell() Out[81]: 1 In [82]: f.read() --------------------------------------------------------------------------- UnicodeDecodeError Traceback (most recent call last) <ipython-input-82-571e9fb02258> in <module> ----> 1 f.read() ~/.pyenv/versions/3.5.3/lib/python3.5/codecs.py in decode(self, input, final) 319 # decode input (taking the buffer into account) 320 data = self.buffer + input --> 321 (result, consumed) = self._buffer_decode(data, self.errors, final) 322 # keep undecoded input until the next call 323 self.buffer = data[consumed:] UnicodeDecodeError: 'utf-8' codec can't decode byte 0xbf in position 0: invalid start byte In [83]: f.seek(2) Out[83]: 2 In [84]: f Out[84]: <_io.TextIOWrapper name='test4' mode='r+' encoding='UTF-8'> In [85]: f.close()
二進位模式下,whence取值範圍及釋義如下:
參數 |
值 |
釋義 |
---|---|---|
whence |
0 |
預設值,表示從頭開始,offest值只能是正整數 |
whence |
1 |
表示從當前位置開始,offest值可正可負 |
whence |
2 |
表示從EOF位置開始,offest值可正可負 |
In [103]: f = open('test4','rb+') In [104]: f Out[104]: <_io.BufferedRandom name='test4'> In [105]: f.tell() # 起始位置 Out[105]: 0 In [106]: f.read() Out[106]: b'aaabbbccc' In [107]: f.tell() Out[107]: 9 In [108]: f.write(b'abc') Out[108]: 3 In [109]: f.seek(0) # 起始位置 Out[109]: 0 In [110]: f.seek(2,1) # 從當前指針位置開始,向後2 Out[110]: 2 In [111]: f.read() Out[111]: b'abbbcccabc' In [112]: f.seek(-2,1) # 從當前位置開始,向前2 Out[112]: 10 In [113]: f.read() Out[113]: b'bc' In [114]: f.tell() Out[114]: 12 In [115]: f.seek(2,2) # 從EOF位置開始,向後2 Out[115]: 14 In [116]: f.seek(0) Out[116]: 0 In [117]: f.seek(-2,2) # 從EOF位置開始,向前2 Out[117]: 10 In [118]: f.tell() Out[118]: 10 In [119]: f.read() Out[119]: b'bc' In [120]: f.seek(-20,2) # OSError --------------------------------------------------------------------------- OSError Traceback (most recent call last) <ipython-input-120-5f6c1973901c> in <module> ----> 1 f.seek(-20,2) OSError: [Errno 22] Invalid argument In [121]: f.close()
二進位模式支援任意起點的便宜,從頭、從未、從中間I之開始,向後seek可以超界,但是向前eek的時候,不能超界,否則拋出異常。