Python檔案處理

來源:互聯網
上載者:User

標籤:必須   選擇   while   用途   linux中   游標移動   直接   python   記錄檔   

一、檔案處理流程
f = open(‘file_management‘,‘r‘,encoding=‘utf-8‘) #預設就是唯讀模式#檔案控制代碼data =f.read()print(data)f.close()
二、檔案開啟檔案

w模式:檔案不存在,建立檔案;檔案存在,直接覆蓋;無論怎樣都是直接建立一個空文檔覆蓋原文檔

f = open(‘new_file‘,‘w‘,encoding=‘utf-8‘)#檔案控制代碼print(f.writable())#寫入的內容必須是是字串f.write(‘11111111\n‘)f.write(‘22222222\n‘)f.writelines([‘3333\n‘,‘44444\n‘])f.close()

a模式:追加模式

f = open(‘new_file‘,‘a‘,encoding=‘utf-8‘)

r+模式:可讀可寫(從游標處開始寫)

f = open(‘new_file‘,‘r+‘,encoding=‘utf-8‘)

其他模式:

# r+  讀寫# w+  寫讀# x+  寫讀# a+  寫讀# “b”表示以位元組方式操作,讀取到的是位元組,寫入的也需要是位元組# rb=r+b# wb=w+b# xb=x+b# ab=a+b
f = open(‘new_file‘,‘rb‘)data = f.read()print(data)print(data.decode(‘utf-8‘))f.close()#windows裡面換行是\r\n;Linux中是\nf =  open(‘new_file‘,‘wb‘)f.write(bytes(‘111\n‘,encoding=‘utf-8‘))f.close()#寫入並覆蓋

 

with 方式開啟,無需關閉

with open(‘file‘,‘w‘) as f,        open(‘file_new‘,‘w‘,ending=‘utf-8‘):    #檔案操作

 

 三、操作檔案的方法

檔案無所謂的修改,本質上都是覆蓋,修改的過程其實是先開啟舊檔案,讀取內容後重新寫入並覆蓋

修改的本質:開啟一個檔案,一行一行地讀入記憶體,讀一行放到新檔案裡面,這個過程中可以修改,完畢後載入到新檔案裡面,覆蓋原有檔案

f = open(‘file_management‘,‘r‘,encoding=‘utf-8‘)#檔案控制代碼print(f.readable())print(‘-‘*80)print(f.readline(),end = ‘‘)print(f.readline(),end = ‘‘)print(f.readline(),end = ‘‘)print(‘-‘*80)print(f.read())f.close()

注意游標的移動,一旦read()也就是讀取了全部內容,游標移動到最後

readline()一次讀一行,注意每一行末尾都有一個斷行符號\n

readlines()全部讀出變成一個列表,一行是一個元素

四、檔案操作的其他方法

1、f.closed   檔案是否關閉

2、flush()刷進硬碟

3、tell()當前位置

4、seek( point )移動游標位置,point除了read是字元,其餘處理時都是處理位元組

new_file1234567890
f = open(‘new_file‘,‘rb‘)  #要想使用第二種和第三種模式,必須用b方式,不指定encodingprint(f.tell())f.seek(10)print(f.tell())f.seek(3)print(f.tell())f.seek(3,0)     #第一種模式,預設模式print(f.tell())f.seek(3,1)     #第二種模式,相對位置print(f.tell())f.seek(-3,2)    #第三種模式:從檔案末尾倒著seekprint(f.tell())print(f.read())
result:0103367b‘890‘

第三種方式的用途:記錄檔的處理

#迴圈檔案的方式for i in f:    print(i)#耗費記憶體for i in f.readlines():    print(i)
new_file2018/8/1  alex  做了一件什麼事2018/8/1  alex  做了一件什麼事2018/8/1  alex  做了一件什麼事2018/8/1  alex  做了一件什麼事2018/8/1  alex  做了一件什麼事2018/8/1  alex  做了一件什麼事2018/8/1  alex  做了一件什麼事2018/8/1  alex  做了一件什麼事2018/8/2  joey  做了一件什麼事
offs = 10    #位移量while True:    f.seek(-offs,2)    data = f.readlines()    if len(data)>1:        print(‘檔案的最後一行是%s‘ %(data[-1].decode(‘utf-8‘)))        break    else:        offs*=2result:檔案的最後一行是2018/8/2  joey  做了一件什麼事

5、read( num )讀入num個字元

6、truncate( num )截取[0, num) (檔案開啟檔案不能選擇w+,以免一開啟就把原檔案清空

Python檔案處理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.