文件內建函數和方法 |
|
open() |
打開文件 |
read() |
讀取 |
readline() |
讀取一行 |
seek() |
文件內移動 |
write() |
寫入 |
close() |
關閉文件 |
寫入文件,此方法類似於unix中的>重定向寫入,會覆蓋源文件:
file1 = open("C:\\Users\\David\\Desktop\\a.txt",'w') |
讀取文件的全部內容:
file1 = open("C:\\Users\\David\\Desktop\\a.txt") |
在文件末尾追加寫入,類似於unix中的>>:
file2 = open("C:\\Users\\David\\Desktop\\a.txt","a") |
讀取文件中的第一行:
file1 = open("C:\\Users\\David\\Desktop\\a.txt") |
逐行讀入,循環處理:
file1 = open("C:\\Users\\David\\Desktop\\a.txt") |
指針在行上一個字符一個字符的向後讀取:
file1 = open("C:\\Users\\David\\Desktop\\a.txt") |
file2.seek(5,0)
#第一個參數代表了偏移位置;
#第二個參數 0表示從文件開頭偏移;1表示從當前位置偏移;2表示從文件結尾偏移