Python fileinput模組詳解

來源:互聯網
上載者:User

標籤:iss   div   檔案   nbsp   描述符   使用   backup   預設   gpo   

 Python的fileinput模組可以快速對一個或多個檔案進行迴圈遍曆。

import fileinputfor line in fileinput.input():    process(line)
fileinput.input([files[, inplace[, backup[, bufsize[, mode[, openhook]]]]]])

files:檔案的路徑列表
inplace:是否返回輸出結果到原檔案中,預設為0不返回,設定為1時返回
backup:備份檔案的副檔名
bufsize:緩衝區大小,python2.7.12版本該參數不再使用
mode:讀寫入模式,只能是讀、寫、讀寫、二進位四種模式,預設是讀模式
openhook:該參數必須是帶兩個參數(檔案名稱和模式)的函數
fileinput.filename()       # 返回當前正在讀取的檔案的名稱
fileinput.fileno()         #返回當前檔案的整數“檔案描述符”fileinput.lineno()         #返回剛剛讀取的行的累計行號fileinput.filelineno()     #返回當前檔案中的行號fileinput.isfirstline()    #判斷讀取的行是不是該檔案的第一行,是就返回true,否則返回falsefileinput.isstdin()        #判斷讀取的是否是sys.stdin的最後一行,是就返回true,否則返回falsefileinput.nextfile()       #關閉當前檔案,以便下一次迭代將讀取下一個檔案的第一行(如果有的話)fileinput.close()          #關閉檔案隊列

 

Python fileinput模組詳解

聯繫我們

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