Python學習日記 13 檔案操作

2021-08-27 03:07:41 字數 3757 閱讀 9727

#能呼叫方法的一定是物件

#*************************

poem = open('南歌子.txt','r',encoding='utf8').read() #'r'讀模式,'read'讀出來,檔案要加字尾名

print(poem)

#**********讀***************

f = open('南歌子.txt','r',encoding='utf8') #讀模式開啟

poem = f.read(10) #讀方式操作 操作 十個字元

print(poem) # 操作

f.close() #關閉檔案 關閉 一定關閉

#**********寫***************

f = open('南歌子.txt','w',encoding='utf8') #'w'寫模式開啟 在建立物件的同時刪除原有內容 如果沒有此檔案則新創立乙個檔案

f.write('***\t') #寫方式操作 操作

f.write('aa a') #緊貼上一串字元顯示

f.close() #關閉檔案 關閉 一定關閉

#**********追加*************

f = open('南歌子.txt','a',encoding='utf8') #'a'追加模式開啟 以後的寫操作,寫入的內容緊貼原有最後字元

f.write('hello')

f.close()

#***************************

f = open('南歌子.txt','r',encoding='utf8')

print(f.readline()) #讀第乙個換行符之前的內容

print(f.readline()) #游標已經到第二行,則這次會列印第二個游標和第乙個游標之間的內容

print(f.readline())

print(f.readline(3)) #列印第四個和第五個換行符之間前三個字元

f.close()

#***************************

f = open('南歌子.txt','r',encoding='utf8')

print(f.readlines())'''

['南歌子詞二首 / 新添聲楊柳枝詞\n',

'【作者】溫庭筠 【朝代】唐\n',

'\n',

'一尺深紅勝曲塵,天生舊物不如新。\n',

'\n', '合歡桃核終堪恨,裡許元來別有人。\n',

'\n', '井底點燈深燭伊,共郎長行莫圍棋。\n',

'\n',

'玲瓏骰子安紅豆,入骨相思知不知。']'''

f.close()

列表形式返回

#*****************************

f = open('南歌子.txt','r',encoding='utf8')

for i in f.readlines():

print(i.strip()) #print本身存在自動換行,所以把原有換行符去掉

f.close()

#*****************************

f = open('南歌子.txt','r',encoding='utf8')

num = 0

for i in f.readlines():

num += 1

if num == 9:

i = ''.join([i.strip(),'lll']) #字串拼接最好用join

print(i.strip())

f.close()

#***********************重要

f = open('南歌子.txt','r',encoding='utf8')

for i in enumerate: #迭代器 節省記憶體 for內部將 f 物件做成迭代器,用一行去一行

print(i.strip())

f.close()

#***********************重要

f = open('南歌子.txt','r',encoding='utf8')

print(f.tell()) #返回當前游標位置

print(f.read(1))

print(f.tell()) #乙個中文3個位元組

f.seek(0) #重要:調整游標位置

print(f.read(1))

#**********************

import time

f = open('南歌子.txt','r',encoding='utf8')

f.flush() #將快取裡的內容更新到磁碟中(做進度條)

****

****

****

****

***import time

for i in range(30):

print('*',end='',flush=true) #進度條

time.sleep(1)

#******************************

f = open('南歌子.txt','a',encoding='utf8')

f.truncate(47) #截斷

f.close()

#*****************************

f = open('南歌子.txt','r+',encoding='utf8') #讀寫,,寫的內容加到最後

print(f.readline())

f.write('lllll')

f.close()

#*******************************

f = open('南歌子.txt','w+',encoding='utf8') #寫讀,,先截斷

print(f.readline()) #第一行為空

f.write('lllll')

f.seek(0) # 游標返回到最開始

print(f.readline())

f.close()

#*************終極問題*****************修改檔案

f = open('南歌子.txt','r',encoding='utf8')

q = open('南歌子2.txt','w',encoding='utf8') #無法對原始檔修改,只能取值然後對變數修改,後存入新的檔案

num = 0

for i in f:

num += 1

if num == 5: #第五行進行修改

i = ''.join([i.strip(),'lll\n']) #在第五行後面加『lll』

q.write(i) #寫入新檔案

f.close()

q.close()

python基礎(13) 檔案

檔案的基本方法 可使用函式open,它位於自動匯入的模組io中。1.open函式將檔名作為唯一必不可少的引數,返回乙個可讀取的檔案物件 open a.py a.py mode r encoding cp936 2.如果要寫入檔案,必須通過指定模式來顯式地指出這一點 3.若不存在該檔案,則會產生如下錯...

python13檔案 13 Python 檔案

概述 嚴格講,檔案不屬於資料型別。02操作 1 開啟檔案 1 基本語法 file open 檔名 mode 引數mode模式可選引數,分為 r讀 w寫 a追加 r w a後面可接第二個引數,b標書二進位制,f open data.bin rb 2 完整語法格式為 open file,mode r b...

python 物件導向 13 檔案

檔案的作用 將資料長期儲存下來,在需要的時候使用 cpu記憶體 硬碟 文字檔案和二進位制檔案 二進位制檔案 在計算機中要操作檔案的套路非常固定,一共包含三個步驟 開啟檔案 讀 寫檔案 關閉檔案 序號函式 方法 說明01 open 開啟檔案,並且返回檔案操作物件 02read 將檔案內容讀取到記憶體 ...