python基礎學習筆記——Python基礎教程(第2版 修訂版)第11章(檔案與素材)

來源:互聯網
上載者:User

標籤:操作   filename   text   code   內容   mode   對象   bre   amr   

#檔案模式 open(name[.mode[.buffering]])

rwab+f=open(r‘c:\text\somefile‘)

#基本檔案方法

#對檔案內容進行迭代

 

f=open(‘somefile.txt‘,‘w‘)  #r是預設的f.write(‘hello‘)f.read(4)f.close()

#管式輸出

$ cat somefile.txt|python somescript.py|sort

#讀寫行

writelinesreadlines

#關閉檔案

#對檔案進行迭代

按位元組處理f=open(filename)while True:         char=f.read(1)         if not char:break         process(char)f.close()#按行操作f=open(filename)while True:        line=f.readline()        if not line:break        process(line)#讀取所有內容f=open(filename)for char in f.read():     process(char)import fileinputfor line in fileinput.input(filenamr)     process(line)$迭代檔案for line inf:     process(line)
file(name[.mode[.buffering]]) #開啟一個檔案並返迴文件對象open(name[.mode[.buffering]]) #file的別名

 

python基礎學習筆記——Python基礎教程(第2版 修訂版)第11章(檔案與素材)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.