Python全棧之路系列之文件操作
Python可以對文件進行查看、創建等功能,可以對文件內容進行添加、修改、刪除,且所使用到的函數在Python3.5.x為 open ,在Python2.7.x同時支持 file 和 open ,但是在3.5.x系列移除了 file 函數。
Python文件打開方式
文件句柄 = open('文件路徑','打開模式')
Ps:文件句柄相當于于變量名,文件路徑可以寫為絕對路徑也可以寫為相對路徑。
Python打開文件的模式
基本的模式
| 模式 | 說明 | 注意事項 |
|---|---|---|
| r | 只讀模式 | 文件必須存在 |
| w | 只寫模式 | 文件不存在則創建文件,文件存在則清空文件內容 |
| x | 只寫模式 | 文件不可讀,文件不存在則創建,存在則報錯 |
| a | 追加模式 | 文件不存在創建文件,文件存在則在文件末尾添加內容 |
帶 + 的模式
| 模式 | 說明 |
|---|---|
| r+ | 讀寫 |
| w+ | 寫讀 |
| x+ | 寫讀 |
| a+ | 寫讀 |
帶 b 的模式
| 模式 | 說明 |
|---|---|
| rb | 二進制讀模式 |
| wb | 二進制寫模式 |
| xb | 二進制只寫模式 |
| ab | 二進制追加模式 |
提示:以b方式打開時,讀取到的內容是字節類型,寫入時也需要提供字節類型
帶 + 帶 b 的模式
| 模式 | 說明 |
|---|---|
| rb+ | 二進制讀寫模式 |
| wb+ | 二進制讀寫模式 |
| xb+ | 二進制只寫模式 |
| ab+ | 二進制讀寫模式 |
Python文件讀取方式
| 模式 | 說明 |
|---|---|
| read([size]) | 讀取文件全部內容,如果設置了size,那么久讀取size字節 |
| readline([size]) | 一行一行的讀取 |
| readlines() | 讀取到的每一行內容作為列表中的一個元素 |
測試的文件名是 hello.tx" ,文件內容為:
Hello Word!
123
abc
456
abc
789
abc
read
代碼:
# 以只讀的方式打開文件hello.txt
f = open("hello.txt","r")
# 讀取文件內容賦值給變量c
c = f.read()
# 關閉文件
f.close()
# 輸出c的值
print(c)
輸出結果:
C:\Python35\python.exe F:/Python_code/sublime/Day06/file.py
Hello Word!
123
abc
456
abc
789
abc
readline
代碼:
# 以只讀模式打開文件hello.txt
f = open("hello.txt","r")
# 讀取第一行
c1 = f.readline()
# 讀取第二行
c2 = f.readline()
# 讀取第三行
c3 = f.readline()
# 關閉文件
f.close()
# 輸出讀取文件第一行內容
print(c1)
# 輸出讀取文件第二行內容
print(c2)
# 輸出讀取文件第三行內容
print(c3)
輸出結果:
C:\Python35\python.exe F:/Python_code/sublime/Day06/file.py
Hello Word!
123
abc
readlines
# 以只讀的方式打開文件hello.txt
f = open("hello.txt","r")
# 將文件所有內容賦值給c
c = f.readlines()
# 查看數據類型
print(type(c))
# 關閉文件
f.close()
# 遍歷輸出文件內容
for n in c:
print(n)
結果
C:\Python35\python.exe F:/Python_code/sublime/Day06/file.py
# 輸出的數據類型
<class 'list'>
Hello Word!
123
abc
456
abc
789
abc
Python文件寫入方式
| 方法 | 說明 |
|---|---|
| write(str) | 將字符串寫入文件 |
| writelines(sequence or strings) | 寫多行到文件,參數可以是一個可迭代的對象,列表、元組等 |
write
代碼:
# 以只讀的模式打開文件write.txt,沒有則創建,有則覆蓋內容
file = open("write.txt","w")
# 在文件內容中寫入字符串test write
file.write("test write")
# 關閉文件
file.close()
write.txt 文件內容為:
test write
writelines
代碼:
# 以只讀模式打開一個不存在的文件wr_lines.txt
f = open("wr_lines.txt","w",encoding="utf-8")
# 寫入一個列表
f.writelines(["11","22","33"])
# 關閉文件
f.close()
wr_lines.txt 文件內容:
Python文件操作所提供的方法
close(self):
關閉已經打開的文件
f.close()
fileno(self):
文件描述符
f = open("hello.txt","r")
ret = f.fileno()
f.close()
print(ret)
執行結果:
flush(self):
刷新緩沖區的內容到硬盤中
f.flush()
isatty(self):
判斷文件是否是tty設備,如果是tty設備則返回 True ,否則返回 False
f = open("hello.txt","r")
ret = f.isatty()
f.close()
print(ret)
返回結果:
C:\Python35\python.exe F:/Python_code/sublime/Day06/file.py
False
readable(self):
是否可讀,如果可讀返回 True ,否則返回 False
f = open("hello.txt","r")
ret = f.readable()
f.close()
print(ret)
返回結果:
C:\Python35\python.exe F:/Python_code/sublime/Day06/file.py
True
readline(self, limit=-1):
每次僅讀取一行數據
f = open("hello.txt","r")
print(f.readline())
print(f.readline())
f.close()
返回結果:
C:\Python35\python.exe F:/Python_code/sublime/Day06/file.py
Hello Word!
123
readlines(self, hint=-1):
把每一行內容當作列表中的一個元素
f = open("hello.txt","r")
print(f.readlines())
f.close()
返回結果:
C:\Python35\python.exe F:/Python_code/sublime/Day06/file.py
['Hello Word!\n', '123\n', 'abc\n', '456\n', 'abc\n', '789\n', 'abc']
-
tell(self):
獲取指針位置
f = open("hello.txt","r")
print(f.tell())
f.close()
返回結果:
C:\Python35\python.exe F:/Python_code/sublime/Day06/file.py
0
seek(self, offset, whence=io.SEEK_SET):
指定文件中指針位置
f = open("hello.txt","r")
print(f.tell())
f.seek(3)
print(f.tell())
f.close()
執行結果
C:\Python35\python.exe F:/Python_code/sublime/Day06/file.py
0
3
seekable(self):
指針是否可操作
f = open("hello.txt","r")
print(f.seekable())
f.close()
執行結果
C:\Python35\python.exe F:/Python_code/sublime/Day06/file.py
True
writable(self):
是否可寫
f = open("hello.txt","r")
print(f.writable())
f.close()
執行結果
C:\Python35\python.exe F:/Python_code/sublime/Day06/file.py
False
writelines(self, lines):
寫入文件的字符串序列,序列可以是任何迭代的對象字符串生產,通常是一個 字符串列表 。
f = open("wr_lines.txt","w")
f.writelines(["11","22","33"])
f.close()
執行結果
read(self, n=None):
讀取指定字節數據,后面不加參數默認讀取全部
f = open("wr_lines.txt","r")
print(f.read(3))
f.seek(0)
print(f.read())
f.close()
執行結果
C:\Python35\python.exe F:/Python_code/sublime/Day06/file.py
112
112233
write(self, s):
往文件里面寫內容
f = open("wr_lines.txt","w")
f.write("abcabcabc")
f.close()
文件內容
abcabcabc
同時打開多個文件
為了避免打開文件后忘記關閉,可以通過管理上下文,即:
with open('log','r') as f:
代碼塊
如此方式,當with代碼塊執行完畢時,內部會自動關閉并釋放文件資源。
在Python 2.7 及以后,with又支持同時對多個文件的上下文進行管理,即:
with open('log1') as obj1, open('log2') as obj2:
pass