Python Basic - python 文件对象的文件交互各类方法描述与实现
生活随笔
收集整理的這篇文章主要介紹了
Python Basic - python 文件对象的文件交互各类方法描述与实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- read()
- widowns下file使用相對路徑
- windows下file使用絕對路徑
- 指定讀取字符個數
- readline()
- 不帶參數
- 帶數字參數
- 多次readline() 并觀察光標(指針)的計數
- readlines()
- 不帶參數
- 帶1個數字參數(數字為1小于一行的字符數)
- 帶1個參數(每數字都小于每行的字符個數)
- 帶1個參數(大于一行的字符個數)
- mode="w":--探索w模式默認的一上來就truncate()操作
- write()--往文件內寫內容
- write()--探索write()的覆蓋寫操作
- seek() 設置指針(光標)的位置
- tell() 獲取當前指針(光標)的位置
- flush() 將緩存中內容寫到磁盤,刷寫磁盤--打印一個進度條顯示
- 方法一:使用sys模塊的stdout()
- truncate() 截短
- truncate 示例:探索truncate只截取指針(光標)之后的內容
- fileno() 查看當前文件的“文件描述符”
- readable()-判斷文件是否可讀
- writeable()-判斷文件是否可寫
read()
widowns下file使用相對路徑
file = open("少年游本意",mode="r",encoding="utf8") #其它參數默認 print(file.read())以下為輸出內容 """ 《少年游.本意》金庸 青衫磊落險峰行。玉壁月華明。馬疾香幽。崖高人遠。微步縠紋生。 誰家子弟誰家院。無計悔多情。虎嘯龍吟。換巢鸞鳳。劍氣碧煙橫。 """ file.close()file:直接填寫了文件名,所以這里使用的是相對路徑,與程序的目錄在一起
windows下file使用絕對路徑
file = open("C:\少年游本意",mode="r",encoding="utf8") #其它參數默認 print(file.read()) file.close()輸出內容與上面一樣,此處不張貼
指定讀取字符個數
import sys #導入一個sys模塊 print(sys.getdefaultencoding()) #打印當前python的默認編碼file = open("C:\少年游本意",mode="r",encoding="utf8") #其它參數默認 print(file.read(8)) #打印前8個位置的字符""" utf-8 《少年游.本意》 """file.close()readline()
不帶參數
file = open("C:\少年游本意",mode="r",encoding="utf8") #其它參數默認 print(file.readline())file.close()###############以下為輸出內容############### """ 《少年游.本意》金庸 """帶數字參數
file = open("C:\少年游本意",mode="r",encoding="utf8") #其它參數默認 print(file.readline(5)) #打印前5個位置的字符file.close()###############以下為輸出內容############### """ 《少年游. """多次readline() 并觀察光標(指針)的計數
file = open("C:\少年游本意",mode="r",encoding="utf8") #其它參數默認 print("還沒開始讀時:",file.tell()) print(file.readline(5)) print("第一次讀:",file.tell()) print(file.readline(5)) print("第二次讀:",file.tell()) print(file.readline(5)) print("第三次讀:",file.tell()) print(file.readline(5)) print("第四次讀:",file.tell()) print(file.readline(5)) print("第五次讀:",file.tell())file.close()###############以下為輸出內容############### """ 還沒開始讀時: 0 《少年游. 第一次讀: 15 本意》金庸 第二次讀: 30第三次讀: 32 青衫磊落險 第四次讀: 47 峰行。玉壁 第五次讀: 62"""readlines()
不帶參數
file = open("C:\少年游本意",mode="r",encoding="utf8") #其它參數默認 print(file.readlines())file.close()###############以下為輸出內容############### """ ['《少年游.本意》金庸\n', '青衫磊落險峰行。玉壁月華明。馬疾香幽。崖高人遠。微步縠紋生。\n', '誰家子弟誰家院。無計悔多情。虎嘯龍吟。換巢鸞鳳。劍氣碧煙橫。']"""帶1個數字參數(數字為1小于一行的字符數)
file = open("C:\少年游本意",mode="r",encoding="utf8") #其它參數默認 print(file.readlines(1)) print(file.readlines(1)) print(file.readlines(1))file.close()###############以下為輸出內容############### """ ['《少年游.本意》金庸\n'] ['青衫磊落險峰行。玉壁月華明。馬疾香幽。崖高人遠。微步縠紋生。\n'] ['誰家子弟誰家院。無計悔多情。虎嘯龍吟。換巢鸞鳳。劍氣碧煙橫。']帶1個參數(每數字都小于每行的字符個數)
file = open("C:\少年游本意",mode="r",encoding="utf8") #其它參數默認 print("還沒開始讀時:",file.tell()) print(file.readlines(5)) print(file.readlines(5)) print(file.readlines(5)) print(file.readlines(5))file.close()###############以下為輸出內容############### """ 還沒開始讀時: 0 ['《少年游.本意》金庸\n'] ['青衫磊落險峰行。玉壁月華明。馬疾香幽。崖高人遠。微步縠紋生。\n'] ['誰家子弟誰家院。無計悔多情。虎嘯龍吟。換巢鸞鳳。劍氣碧煙橫。'] [] """帶1個參數(大于一行的字符個數)
file = open("C:\少年游本意",mode="r",encoding="utf8") #其它參數默認 print("還沒開始讀時:",file.tell()) print(file.readlines(11)) print(file.readlines(5)) print(file.readlines(5)) print(file.readlines(5))file.close()###############以下為輸出內容############### """ 還沒開始讀時: 0 ['《少年游.本意》金庸\n', '青衫磊落險峰行。玉壁月華明。馬疾香幽。崖高人遠。微步縠紋生。\n'] ['誰家子弟誰家院。無計悔多情。虎嘯龍吟。換巢鸞鳳。劍氣碧煙橫。'] [] [] """mode=“w”:–探索w模式默認的一上來就truncate()操作
#################w之前先讀取文件的內容并輸出查看一遍###################### file = open("少年游本意",mode="r",encoding="utf8") #其它參數默認 print("w模式之前讀取文件內容:\n",file.read()) file.close() #查看之后一定要記得關閉這個打開操作#################open()操作執行w模式###################### file = open("少年游本意",mode="w",encoding="utf8") #其它參數默認file.close()#################w之后先讀取文件的內容并輸出查看一遍###################### file = open("少年游本意",mode="r",encoding="utf8") #其它參數默認 print("w模式之后讀取文件內容:\n",file.read()) file.close()###############以下為輸出內容############### """ w模式之前讀取文件內容:《少年游.本意》金庸 青衫磊落險峰行。玉壁月華明。馬疾香幽。崖高人遠。微步縠紋生。 誰家子弟誰家院。無計悔多情。虎嘯龍吟。換巢鸞鳳。劍氣碧煙橫。 w模式之后讀取文件內容:"""### mode="w":--探索w模式默認的一上來就truncate()操作 ```python #################w之前先讀取文件的內容并輸出查看一遍###################### file = open("少年游本意",mode="r",encoding="utf8") #其它參數默認 print("w模式之前讀取文件內容:\n",file.read()) file.close() #查看之后一定要記得關閉這個打開操作#################open()操作執行w模式###################### file = open("少年游本意",mode="w",encoding="utf8") #其它參數默認file.close()#################w之后先讀取文件的內容并輸出查看一遍###################### file = open("少年游本意",mode="r",encoding="utf8") #其它參數默認 print("w模式之后讀取文件內容:\n",file.read()) file.close()###############以下為輸出內容############### """ w模式之前讀取文件內容:《少年游.本意》金庸 青衫磊落險峰行。玉壁月華明。馬疾香幽。崖高人遠。微步縠紋生。 誰家子弟誰家院。無計悔多情。虎嘯龍吟。換巢鸞鳳。劍氣碧煙橫。 w模式之后讀取文件內容:"""write()–往文件內寫內容
write()–探索write()的覆蓋寫操作
第一次使用wirite()寫一行,然后再寫一行,最后查看數據只有最后一次寫的一行。此功能可用于臨時的文件,數據不用保存,但是需要臨時寫一下文件。
seek() 設置指針(光標)的位置
tell() 獲取當前指針(光標)的位置
file = open("C:\少年游本意",mode="r",encoding="utf8") #其它參數默認 print("還沒開始讀時:",file.tell()) print(file.readline(5)) print("第一次讀:",file.tell()) print(file.readline(5)) print("第二次讀:",file.tell()) print(file.readline(5)) print("第三次讀:",file.tell()) print(file.readline(5)) print("第四次讀:",file.tell()) print(file.readline(5)) print("第五次讀:",file.tell())file.close()###############以下為輸出內容############### """ 還沒開始讀時: 0 《少年游. 第一次讀: 15 本意》金庸 第二次讀: 30第三次讀: 32 青衫磊落險 第四次讀: 47 峰行。玉壁 第五次讀: 62 """flush() 將緩存中內容寫到磁盤,刷寫磁盤–打印一個進度條顯示
方法一:使用sys模塊的stdout()
import sys,timefor i in range(50):sys.stdout.write("*")sys.stdout.flush()time.sleep(1) """ ************************************************** 結果是一個動態的效果 """方法二:使用print()方法
for i in range(50):print("*",flush=True,end="")time.sleep(1)truncate() 截短
truncate 示例:探索truncate只截取指針(光標)之后的內容
示例一:未使用seek定位光標內容
示例二:使用seek定位光標內容
fileno() 查看當前文件的“文件描述符”
file = open("少年游本意",mode="w",encoding="utf8") #其它參數默認 print(file.fileno()) file.close()file = open("少年游本意",mode="r",encoding="utf8") #其它參數默認 print(file.fileno()) file.close() """ 3 3 """readable()-判斷文件是否可讀
file = open("少年游本意",mode="w",encoding="utf8") #其它參數默認 print(file.readable()) print(file.writable()) file.close()file = open("少年游本意",mode="r",encoding="utf8") #其它參數默認 print(file.readable()) print(file.writable()) file.close() """ False True True False """writeable()-判斷文件是否可寫
file = open("少年游本意",mode="w",encoding="utf8") #其它參數默認 print(file.readable()) print(file.writable()) file.close()file = open("少年游本意",mode="r",encoding="utf8") #其它參數默認 print(file.readable()) print(file.writable()) file.close() """ False True True False """總結
以上是生活随笔為你收集整理的Python Basic - python 文件对象的文件交互各类方法描述与实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 深圳云计算培训:如何入门云计算?
- 下一篇: 维特比算法 python_维特比算法理解