Re: [問題] 使用for迴圈讀取大量檔案內容

看板Python作者 (阿之)時間12年前 (2013/10/03 15:47), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串3/3 (看更多)
※ 引述《Nsquare (N^2)》之銘言: : 大家好,因為處理實驗data需要,要在linux作業系統下讀取大量檔案內容, : 並且將列轉行再輸出處理。 : 程式碼如下: : file_list = open('file_list.txt') : f = file_list.readlines() : for i in f: : s = open(i).read() : print(s.splitlines(False)) : file_list.close() : 但是在for迴圈讀取這步驟會因為換行符號\n而無法讀取檔案 : 請問有其他更好的方式能夠讀取list中的以變數表示的檔名嗎? 根據原po問題, 我會改成這樣的寫法, file_list = list() with open('file_list.txt', 'r') as f: // 已知是檔案名稱所以去除右空白與換行 file_list.append(f.readline().rstrip()) for file_name in file_list: with open(file_name, 'r') as f: // do something.... 提供參考 :) -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 202.89.121.16
文章代碼(AID): #1IJI4Bhc (Python)
文章代碼(AID): #1IJI4Bhc (Python)