python3.x 基礎三:文件IO

打開文件的兩種方式

1.直接打開文件並賦值給變量,打開後獲得操做句柄,但不會自動關閉java

  • file = open('文件名‘,'打開模式',’編碼‘)
  • 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")

 打開文件的8種模式

    ========= ===============================================================
    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.'+',打開硬盤文件讀寫編碼

  • r+,打開並寫入
  • >>> 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'

     

  • w+,打開文件讀寫,文件存在則覆蓋,不存在則建立
  • >>> 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'
  • a+,打開文件讀寫,存在則將指針置於末尾,不存在則建立新文件
  • >>> 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'

     

  • rb+, wb+, ab+ 對象是二進制,其餘以上面同樣

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

讀取文件的4個read,默認從頭開始讀,並將將指針留在行尾

1.fd.read(size)

  • 默認省略size,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)

  • 默認一行行讀取,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)

  • 講文本所有轉換成列表,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, /) 關閉打開的文件

  •  |      Flush and close the IO object.
  •  |      
  •  |      This method has no effect if the file is already closed.

detach(self, /) 幹嗎用?

  •  |      Separate the underlying buffer from the TextIOBase and return it.
  •  |      After the underlying buffer has been detached, the TextIO is in an
  •  |      unusable state.
>>> 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, /) 返回文件描述符,幹嗎用?

  •  |      Returns underlying file descriptor if one exists.
  •  |      OSError is raised if the IO object does not use a file descriptor.
>>> 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, /) 將緩存當即寫入硬盤,提升效率

  •  |      Flush write buffers, if applicable.
  •  |      This is not implemented for read-only and non-blocking streams.
import time
import sys
for i in range(40):
    sys.stdout.write("#")
    sys.stdout.flush()
    time.sleep(0.1)

 

isatty(self, /) 是否鏈接到終端設備

  •  |      Return whether this is an 'interactive' stream.
  •  |      Return False if it can't be determined.

seekable(self, /)

  •  |      Return whether object supports random access.
  •  |      If False, seek(), tell() and truncate() will raise OSError.
  •  |      This method may need to do a test seek().

truncate(self, pos=None, /)

  •  |      Truncate file to size bytes.
  •  |      File pointer is left unchanged.  Size defaults to the current IO
  •  |      position as reported by tell().  Returns the new size.
>>> 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.一行一行讀取內存,修改完畢以後寫入新文件,用新文件覆蓋舊文件

練習一:實現sed替換功能

 

練習二:修改haproxy配置文件

相關文章
相關標籤/搜索