python中file物件的常用方法

2021-09-24 19:49:06 字數 3555 閱讀 5535

python open() 方法用於開啟乙個檔案,並返回檔案物件,在對檔案進行處理過程都需要使用到這個函式,如果該檔案無法被開啟,會丟擲 oserror。(使用 open() 方法一定要保證關閉檔案物件,即呼叫 close() 方法)

open(file, mode='r', buffering=-1, encoding=none, errors=none, newline=none, 

closefd=true, opener=none)

close()方法

close()方法用於關閉乙個已開啟的檔案,關閉後的檔案不能再進行讀寫操作, 否則會觸發 valueerror 錯誤。close() 方法允許呼叫多次。

語法:fileobject.close();
file = open("hello.txt","wb")

print(file.name)

file.close()

# 輸出:hello.txt

read() 方法

read() 方法用於從檔案讀取指定的位元組數,如果未給定或為負則讀取所有。

首先自己建立乙個hello.txt文件(自定義),文件中的內容是hello world。

file = open("hello.txt", "r")

read1 = file.read()

print(read1)

file.close()

# 輸出:hello world

write()方法

write()方法用於向檔案中寫入指定字串。

向乙個空的hello.txt文件中寫入hello world。

file = open("hello.txt", "w")

file.write("hello world")

file.close()

writelines() 方法

writelines() 方法用於向檔案中寫入一串行的字串。這一串行字串可以是由迭代物件產生的,如乙個字串列表。換行需要制定換行符 \n。

file = open("hello.txt", "w")

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

file.writelines(con)

file.close()

# hello.txt檔案中寫入ab

c

flush() 方法

flush() 方法是用來重新整理緩衝區的,即將緩衝區中的資料立刻寫入檔案,同時清空緩衝區,不需要是被動的等待輸出緩衝區寫入。

一般情況下,檔案關閉後會自動重新整理緩衝區,但如果你需要在關閉前重新整理它,就可以使用 flush() 方法。

file = open("hello.txt", "wb")

print("檔名:", file.name)

file.flush()

file.close()

# 檔名: hello.txt

readline() 方法

readline() 方法用於從檔案讀取整行,包括 「\n」 字元。如果指定了乙個非負數的引數,則返回指定大小的位元組數,包括 「\n」 字元。

# hello.txt的內容

123456

789

file = open("hello.txt", "r")

content = file.readline()

print(content)

file.close()

# 輸出hello.txt檔案的第一行:123

readlines() 方法

readlines() 方法用於讀取所有行(直到結束符 eof)並返回列表,該列表可以由 python 的 for… in … 結構進行處理。如果碰到結束符 eof 則返回空字串。

file = open("hello.txt", "r")

content = file.readlines()

print(content)

file.close()

# 輸出:['123\n', '456\n', '789']

seek() 方法

seek() 方法用於移動檔案讀取指標到指定位置。

fileobject.seek(offset[, whence])
假設hello.txt檔案中的內容是abcdefghijk,那麼我們使用 seek() 方法來移動檔案指標試試:

file = open("hello.txt", "r")

file.seek(3) #檔案指標移動到第三位,從第四位開始讀

print(file.read()) # 輸出:defghijk

file.seek(5)

print(file.read()) # 輸出:fghijk

file.close()

tell() 方法

tell() 方法返回檔案的當前位置,即檔案指標當前位置。

file = open("hello.txt", "r")

file.seek(4) #檔案指標移動到第三位,從第四位開始讀

print(file.tell()) # 4

file.close()

fileno()方法

fileno() 方法返回乙個整型的檔案描述符(file descriptor fd 整型),可用於底層作業系統的 i/o 操作。

file = open("hello.txt", "w")

con = file.fileno()

print(con)

file.close()

# 輸出:3

isatty()方法

如果檔案連線到乙個終端裝置返回 true,否則返回 false。

file = open("hello.txt", "w")

con = file.isatty()

print(con)

file.close()

# 輸出:false

truncate() 方法

truncate() 方法用於截斷檔案,如果指定了可選引數 size,則表示截斷檔案為 size 個字元。 如果沒有指定 size,則從當前位置起截斷;截斷之後 size 後面的所有字元被刪除。

# hello.txt文字ab

cde

file = open("hello.txt", "r")

con = file.readline()

print(con) # 輸出:a

file.truncate() # 截斷剩下的字串

con = file.readline()

print(con)

file.close()

python中file物件的常用方法

python open 方法用於開啟乙個檔案,並返回檔案物件,在對檔案進行處理過程都需要使用到這個函式,如果該檔案無法被開啟,會丟擲 oserror。使用 open 方法一定要保證關閉檔案物件,即呼叫 close 方法 open file mode r buffering 1 encoding no...

C 中的常成員和常物件函式

1 建立物件時新增const關鍵字,這個物件就不可再修改,就有了常屬性,就意味著整個物件中的所有東西都不能修改。我們知道被const修飾的物件一旦被初始化了就不可改變,來看乙個例子 include include using namespace std class dog void show obj...

File物件的方法

createnewfile 建立乙個檔案 mkdir 建立乙個資料夾 mkdirs 建立路徑中不存在的所有資料夾 renameto file file 可以用來重新命名檔案,也可以用來移動檔案 exists 判斷檔案是否存在 isfile 判斷是否是普通檔案 isdirectory 判斷是否是資料夾...