Python File 對象常用的函數

平臺:Windows10
軟件:Python 3.7
百度網盤鏈接:☛
提取碼:60mn


目錄

Python File 對象常用的函數(file對象使用open函數來創建)

1、file.write(str)

2、file.writelines(sequence)

3、file.read([size])

4、file.readline([size])

5、file.readlines([sizeint])

6、file.close()

7、file.flush()

8、file.seek(offset[,from])

9、file.tell()

10、file.next()

11、file.truncate([size])

12、file.fileno()

13、file.isatty()


Python File 對象常用的函數file對象使用open函數來創建

 

write1.txt:

 write sentence

write2.txt:(每行5個字符)

1_a_2
3_b_4
5_c_6


1、file.write(str)

作用:

將任何字符串寫入一個打開的文件。

例如:

import os
os.chdir(r"c:\users\安東省\desktop")  #改變當前工作目錄
fo=open("write1.txt","w")    #使用相對路徑,在桌面新建文件write1
str="write sentence"         #建立字符串
fo.write(str)     #將字符串寫入
print(fo.name)    #獲取文件名
fo.close()        #關閉文件

2、file.writelines(sequence)

作用:

向文件寫入一個序列字符串列表或字符串。

例如:

import os
os.chdir(r"c:\users\安東省\desktop")  #改變當前工作目錄
fo=open("write2.txt","w+")    #使用相對路徑,在桌面新建文件write2

str=["1_","a_","2\n","3_","b_","4\n","5_","c_","6"] #創建字符列表
fo.writelines(str)    #將字符列表寫入文件

print(fo.tell())        #獲取文件當前位置,文件指針指向文件尾
print(fo.seek(0,0)) #使用seek函數將文件指針移動到文件開頭處

print(fo.name)    #獲取文件名
print(fo.read())    #讀取文件內容

fo.close()  #關閉文件

結果:

====
19
0
write2.txt
1_a_2
3_b_4
5_c_6
>>> 


3、file.read([size])

作用:

從一個打開的文件中讀取size個字符,默認讀取全部。

例如:

import os

os.chdir(r"c:\users\安東省\desktop")  #改變當前工作目錄
fo=open("write1.txt","w+")    #使用相對路徑,在桌面新建文件write1

print(fo.read())
fo.close()

結果:

====
write sentence
>>> 

4、file.readline([size])

作用:

每次讀取一行,包括換行符,size <= 行內容。

例如:

import os
os.chdir(r"c:\users\安東省\desktop")  #改變當前工作目錄
fo=open("write2.txt","r")    #使用相對路徑,在桌面新建文件write2

print(fo.readline())
fo.close()

結果:

====
1_a_2


>>> 


5、file.readlines([sizeint])

作用:

讀取所有行並返回列表。

例如:

import os
fo=open(r"C:\Users\安東省\Desktop\write2.txt","r")
for line in fo.readlines():
    print(line)
fo.close()

結果:

====
1_a_2

3_b_4

5_c_6
>>> 


6、file.close()

作用:

關閉文件,關閉後不能進行讀寫操作。

關閉文件是一個良好的習慣,如果不關閉你寫入的內容就不會寫到文件裏等等。

例如:

以上代碼均可參考!

7、file.flush()

作用:

用來刷新緩衝區的,即將緩衝區中的數據立刻寫入文件,同時清空緩衝區,不需要被動的等待輸出緩衝區寫入。

一般情況下,文件關閉後會自動刷新緩衝區,但有時你需要在關閉前刷新它,這時就可以使用 flush() 方法。

例如:

import os
os.chdir(r"c:\users\安東省\desktop")  #改變當前工作目錄
fo=open("temp.txt","w")
fo.write("hello python")
fo.flush()
print(fo.name)
fo.close()

結果:

你打開的文件名爲: temp.txt


8、file.seek(offset[,from])

作用:

改變當前文件的位置

offset: 表示要移動的字節數(開頭從0開始)

from:默認值爲 0:代表從文件開頭開始算起,1代表從當前位置開始算起,2代表從文件末尾算起。

例如:

import os
os.chdir(r"c:\users\安東省\desktop")  #改變當前工作目錄
fo=open("write1.txt","r")

print("練習1")
print(fo.read())    #此時文件指針在文件尾

print("\n練習1.1")
fo.seek(7,0)    #0:從文件開頭讀入,7:從文件開頭向右移動到位置7處,其實當前位置爲第8個字節
print(fo.read())

f=open("write2.txt","r")
print("\n練習2")
f.seek(7)        #位置移動到7處,其實當前位置爲第8個字節
print(f.read())

f.close()
fo.close()

結果:

====
練習1
write sentence

練習1.1
entence

練習2
3_b_4
5_c_6
>>> 

解題思路:

 

9、file.tell()

作用:

返回文件內的當前位置(下一次從此位置之後讀寫等)

例如:

import os
os.chdir(r"c:\users\安東省\desktop")  #改變當前工作目錄
fo=open("write1.txt","r")
print(fo.tell())    #0 文件剛打開,指針指向文件開始位置
print(fo.read(2))    #從開頭開始自左向右讀取兩個字符

print(fo.tell())    #2 read讀了兩個字符,當前位置指針指向2處,下一次讀從位置2處之後讀取。
print(fo.read())    #位置2之後讀取,一直到結尾
print(fo.tell())    #14 文件總共字節數
fo.close()

結果:

0
wr
2
ite sentence
14

10、file.next()

作用:

返回文件下一行

例如:

import os
os.chdir(r"c:\users\安東省\desktop")  #改變當前工作目錄
fo=open("write2.txt","r")    #使用相對路徑打開write2文本文件
line=next(fo)
print(line)    #返回文件第一行
print(line)    #返回文件第二行
fo.close()

結果:

====
1_a_2

1_a_2

>>> 


11、file.truncate([size])

作用:

截取文件,截取之後的內容將會被刪除:

①默認爲當前文件位置

②截取大於文件內容,自動補充空格或者其他字母

例如:

import os
os.chdir(r"c:\users\安東省\desktop")
fo=open("write1.txt","r+")    #截取有改動文件內容,所以要用到r+(讀寫)

print(fo.read())    #原文件內容
print(fo.truncate(5))    #截取前5個字節,大於第5個字節之後進行刪除
print(fo.tell())    #使用tell函數返回文件當前位置
fo.seek(0,0)    #使用seek函數把文件指針移動到文件開頭處
print(fo.read())    #字節從1開始,此時文件大小爲5字節

fo.close()    #關閉文件

結果:

write sentence
5
14
write
>>> 


12、file.fileno()

作用:

返回一個整形的文件描述符,可以用在如os模塊的read函數等一些底層操作上

例如:

結果:

====
文件名: write1.txt
文件描述符: 3
>>> 


13、file.isatty()

作用:

 文件連接到終端返回true,則返回false

例如:

import os
os.chdir(r"c:\users\安東省\desktop")  #改變當前工作目錄
fo=open("write1.txt","r")    #使用相對路徑打開write1文本文件
print(fo.name)
print(fo.isatty())
fo.close()

結果:

====
write1.txt
False
>>> 


 作者:安東省心

時間:2019/9/28

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章