自學Python之路-Python基礎+模塊+面向對象
自學Python之路-Python網絡編程
自學Python之路-Python併發編程+數據庫+前端
自學Python之路-djangohtml
操做文件時,通常須要經歷以下步驟:前端
open('文件路徑', '模式')
打開文件時,須要指定文件路徑和以何等方式打開文件,打開後,便可獲取該文件句柄,往後經過此文件句柄對該文件操做。打開文件的模式有:python
"+" 表示能夠同時讀寫某個文件數據庫
"b"表示以字節的方式操做django
注:以b方式打開時,讀取到的內容是字節類型,寫入時也須要提供字節類型編程
模式 | 描述 |
---|---|
r | 以只讀方式打開文件。文件的指針將會放在文件的開頭。這是默認模式。 |
rb | 以二進制格式打開一個文件用於只讀。文件指針將會放在文件的開頭。這是默認模式。 |
r+ | 打開一個文件用於讀寫。文件指針將會放在文件的開頭。 |
rb+ | 以二進制格式打開一個文件用於讀寫。文件指針將會放在文件的開頭。 |
w | 打開一個文件只用於寫入。若是該文件已存在則將其覆蓋。若是該文件不存在,建立新文件。 |
wb | 以二進制格式打開一個文件只用於寫入。若是該文件已存在則將其覆蓋。若是該文件不存在,建立新文件。 |
w+ | 打開一個文件用於讀寫。若是該文件已存在則將其覆蓋。若是該文件不存在,建立新文件。 |
wb+ | 以二進制格式打開一個文件用於讀寫。若是該文件已存在則將其覆蓋。若是該文件不存在,建立新文件。 |
a | 打開一個文件用於追加。若是該文件已存在,文件指針將會放在文件的結尾。也就是說,新的內容將會被寫入到已有內容以後。若是該文件不存在,建立新文件進行寫入。 |
ab | 以二進制格式打開一個文件用於追加。若是該文件已存在,文件指針將會放在文件的結尾。也就是說,新的內容將會被寫入到已有內容以後。若是該文件不存在,建立新文件進行寫入。 |
a+ | 打開一個文件用於讀寫。若是該文件已存在,文件指針將會放在文件的結尾。文件打開時會是追加模式。若是該文件不存在,建立新文件用於讀寫。 |
ab+ | 以二進制格式打開一個文件用於追加。若是該文件已存在,文件指針將會放在文件的結尾。若是該文件不存在,建立新文件用於讀寫。網絡 |
x | 若是文件存在則報錯,若是不存在,則建立文件並寫內容 |
class TextIOWrapper(_TextIOBase): """ Character and line based layer over a BufferedIOBase object, buffer. encoding gives the name of the encoding that the stream will be decoded or encoded with. It defaults to locale.getpreferredencoding(False). errors determines the strictness of encoding and decoding (see help(codecs.Codec) or the documentation for codecs.register) and defaults to "strict". newline controls how line endings are handled. It can be None, '', '\n', '\r', and '\r\n'. It works as follows: * On input, if newline is None, universal newlines mode is enabled. Lines in the input can end in '\n', '\r', or '\r\n', and these are translated into '\n' before being returned to the caller. If it is '', universal newline mode is enabled, but line endings are returned to the caller untranslated. If it has any of the other legal values, input lines are only terminated by the given string, and the line ending is returned to the caller untranslated. * On output, if newline is None, any '\n' characters written are translated to the system default line separator, os.linesep. If newline is '' or '\n', no translation takes place. If newline is any of the other legal values, any '\n' characters written are translated to the given string. If line_buffering is True, a call to flush is implied when a call to write contains a newline character. """ def close(self, *args, **kwargs): # real signature unknown 關閉文件 pass def fileno(self, *args, **kwargs): # real signature unknown 文件描述符 pass def flush(self, *args, **kwargs): # real signature unknown 刷新文件內部緩衝區 pass def isatty(self, *args, **kwargs): # real signature unknown 判斷文件是不是贊成tty設備 pass def read(self, *args, **kwargs): # real signature unknown 讀取指定字節數據 pass def readable(self, *args, **kwargs): # real signature unknown 是否可讀 pass def readline(self, *args, **kwargs): # real signature unknown 僅讀取一行數據 pass def seek(self, *args, **kwargs): # real signature unknown 指定文件中指針位置 pass def seekable(self, *args, **kwargs): # real signature unknown 指針是否可操做 pass def tell(self, *args, **kwargs): # real signature unknown 獲取指針位置 pass def truncate(self, *args, **kwargs): # real signature unknown 截斷數據,僅保留指定以前數據 pass def writable(self, *args, **kwargs): # real signature unknown 是否可寫 pass def write(self, *args, **kwargs): # real signature unknown 寫內容 pass def __getstate__(self, *args, **kwargs): # real signature unknown pass def __init__(self, *args, **kwargs): # real signature unknown pass @staticmethod # known case of __new__ def __new__(*args, **kwargs): # real signature unknown """ Create and return a new object. See help(type) for accurate signature. """ pass def __next__(self, *args, **kwargs): # real signature unknown """ Implement next(self). """ pass def __repr__(self, *args, **kwargs): # real signature unknown """ Return repr(self). """ pass buffer = property(lambda self: object(), lambda self, v: None, lambda self: None) # default closed = property(lambda self: object(), lambda self, v: None, lambda self: None) # default encoding = property(lambda self: object(), lambda self, v: None, lambda self: None) # default errors = property(lambda self: object(), lambda self, v: None, lambda self: None) # default line_buffering = property(lambda self: object(), lambda self, v: None, lambda self: None) # default name = property(lambda self: object(), lambda self, v: None, lambda self: None) # default newlines = property(lambda self: object(), lambda self, v: None, lambda self: None) # default _CHUNK_SIZE = property(lambda self: object(), lambda self, v: None, lambda self: None) # default _finalizing = property(lambda self: object(), lambda self, v: None, lambda self: None) # default
1. write(self, *args, **kwargs) 寫數據併發
read(self, *args, **kwargs) 讀取指定字符數據app
f=open('test.log','w') f.write('afaefafafafaf') # 先寫入文件 f.close()
f=open('test.log','r') ret=f.read(6) # 讀取6個字符 f.close() print(ret)
輸出 afafae
2. readline(self, *args, **kwargs) 僅讀取一行
3. tell(self, *args, **kwargs) 獲取指針位置(按字節)
4. seek(self, *args, **kwargs) 指定文件中指針位置
5. truncate(self, *args, **kwargs) 截斷數據,僅保留指定以前數據,更新原文件 ide
這步操做並非必須的,若是使用open()函數打開文件,那麼要記得最終close()文件
若是使用with 語句打開的文件,則不須要。在with 語句主體語句執行完以後,會自動調用close()來關閉文件句柄。語法以下:
with open('db1', 'r', encoding="utf-8") as f1: for line in f1: print(line)
同時打開兩個文件,以下:
with open('file1', 'r', encoding="utf-8") as f1, open("file2", 'w',encoding="utf-8") as f2: pass