1. 程式人生 > >學習筆記 : python 文件操作

學習筆記 : python 文件操作

tell position 如果 can nbsp 需要 文件指針 xxxxx 學習

1.如果文件路徑帶有 \

比如 open(‘c:\python\test.txt‘)

會報:SyntaxError: (unicode error) ‘unicodeescape‘ codec can‘t decode bytes in position 2-3: truncated \UXXXXXXXX escape

所以需要改成:

1)前面加 r ==> open(r‘c:\python\test.txt‘)

2)轉義==> open(‘c:\\python\\test.txt‘)

2.常用的幾個方法:

1) open() 打開文件

2) read() 輸入 ==> 相對於程序來說,是輸入,也就是讀取

3) readline() 輸入一行

4) seek() 移動位置 ==> 移動的位置相對於光標前後移動

5) write() 輸出

6) close() 關閉文件操作

7) tell() 文件指針(讀取)位置

學習筆記 : python 文件操作