操作流python Python基礎 檔案和流操作

2021-10-11 05:05:11 字數 3401 閱讀 9390

python開啟檔案可以用open函式

語法:open(filename, mode[, buffering]),返回乙個檔案物件

mode為檔案模式,buffering為緩衝,都是可選引數

環境及版本

windows 10 x64

python 2.7

pycharm 4.5.1

檔案模式(mode)值描述

'r'唯讀模式

'w'只寫模式(會覆蓋掉已經有的內容)

'a'追加模式(向已有的內容後面進行追加)

'b'二進位制模式

't'文字模式

讀寫模式

'u'通用匹配換行符模式

檔案模式組合值描述

檔案不存在

'r'或'rt'

唯讀模式(default)

報錯'rb'

唯讀模式(針對二進位制檔案)

報錯'w'或'wt'

只寫模式(覆蓋已有內容)

建立'wb'

只寫模式(針對二進位制檔案,覆蓋已有內容)

建立'a'

只寫模式(向已有的內容後面進行追加)

建立'r+'

讀寫模式(覆蓋)

報錯'w+'

讀寫模式(覆蓋)

建立'a+'

讀寫模式(向已有的內容後面進行追加)

建立緩衝(buffering)值描述

0或false

無緩衝,讀寫操作直接針對磁碟

1或true

有緩衝,只有使用flush或者close時才會寫入磁碟

大於1數字表示緩衝區大小(位元組)

任意負數

表示預設緩衝區大小

讀和寫方法名

描述read([size])

以字串形式返回資料,可選引數size可以指定讀取的位元組數,如果未指定表示返回全部資料

write(str)

將字串寫入檔案

# 讀檔案

file_read = open(file_path, 'r+')

print file_read.read()

file_read.close()

# 寫檔案

file_wtite = open(file_path, 'a+')

file_wtite.write("this is a test!".decode("utf-8")) # 中文utf-8

file_wtite.close()

讀寫行方法名

描述readline()

以當前位置,從檔案中讀取一行,以字串返回這行資料,offset指向下一行起始位置

readlines([size])

將檔案返回行為列表(list型別),可選引數size用於指定返回的行數;如果size未指定,表示返回所有行數

writelines()

將乙個字串列表一次性寫入到檔案中,不會加換行符

# 讀一行資料

file_readline = open(file_path, 'r+')

print file_readline.readline().decode("utf-8")

# 讀指定行數

lines = file_readline.readlines(2)

for line in lines:

print line

file_readline.close()

# 寫入乙個字串列表

str_list = ["a\n", "b\n", "c\n"]

file_writelines = open(file_path, 'a+')

file_writelines.writelines(str_list)

file_writelines.close()

隨機訪問

方法名描述

seek(offset[, whence])

把當前位置移動到offset和whence定義的位置。offset時乙個位元組(字元)偏移量,whence預設0表示偏移量從檔案的開頭計算;whence為1表示相對於當前位置偏移;whence為2表示從檔案的尾部向前偏移

tell()

返回當前檔案的位置

# offset偏移

offset_path = os.path.abspath('../doc/offset_test.txt')

file_offset = open(offset_path, 'w')

file_offset.write("abcdefg")

file_offset.seek(2)

file_offset.write('12346')

file_offset.close()

file_offset = open(offset_path)

print file_offset.read()

# tell返回當前檔案位置

file_tell = open(offset_path, 'w')

file_tell.seek(3)

print file_tell.tell()

關閉、清空緩衝

方法名描述

close()

關閉檔案流

flush()

強制寫入磁碟,並清空緩衝區

通常檔案、流的關閉應該在try/except/finally語句中

# 此處開啟檔案

try:

# 檔案操作

except:

# 捕獲可能出現的異常

finally:

# 關閉檔案

file.close()

還可以使用with語句(上下文管理器)

檔案在語句結束後會自動關閉,即使引發了異常也是這樣

with open("file_path") as f:

do_something(f)

檔案迭代

按位元組迭代

file_name = open(file_path)

while true:

char = file_name.read(1)

if not char:

break

process(char)

file_name.close()

按行迭代

file_name = open(file_path)

while true:

line = file_name.readline()

if not line:

break

process(line)

file_name.close()

迭代全文

file_name = open(file_path)

for line in file_name:

process(line)

file_name.close()

參考:python基礎教程(第2版·修訂版)

python python操作xml檔案

最近的工作和xml檔案打交道比較多,就想著看能否通過python去修改xml檔案中的屬性值,然後python中果然是有現成的庫可以使用,先來看一段 usr bin python try import xml.etree.celementtree as et except importerror im...

Python Python刪除檔案操作

在深度學習研究和工程應用中,我們經常需要製作資料集,對資料集進行清洗,排除髒資料,這時候就需要用到python的刪除檔案操作了。我們必須匯入 os 模組,並執行其 os.remove 函式。我們直接舉乙個例子 假設removefile是乙個set,我們將想要刪除的資料路徑存在裡面 forfile i...

Python Python序列的切片操作

序列 consequence 是 python 中一種資料結構,這種資料結構根據索引來獲取序列中的物件。一般說來,具有序列結構的資料型別都可以使用 index,len,max,min,in,切片。如 a abcdefg print len a 7print max a gprint min a ap...