Re: [問題] 用file open抓TXT開的問題

看板Python作者 (←這人是超級笨蛋)時間8年前 (2016/05/02 17:05), 8年前編輯推噓1(100)
留言1則, 1人參與, 最新討論串3/3 (看更多)
※ 引述《doomleika (iSuck)》之銘言: : import os : from os.path import join, abspath : : def get_path(directory, file): : return abspath(join(directory, file)) : : # Put your directory here : directory = "C:/path/to/your/directory" : : directories = os.walk(directory) : text_list = [] : : for directory, _, files in directories: : for a_file in files: : file_path = get_path(directory, a_file) : with open(file_path, 'r') as f: : text_list.append(f.read())

05/02 10:34,
請問Python 3要怎麼寫 ._.?
05/02 10:34
大概像這樣 import pathlib dirpath = pathlib.Path('C:/path/to/your/directory') text_list = [p.read_text() for p in dirpath.rglob('*') if p.is_file()] 其實 glob 在 Python 2 也有(glob module), 不過 pathlib 在讀檔上方便很多 -- 「我最想要的同伴嘛,首先是要笑口常開,其次是我們能永遠不會發生誤會。 如果這些都能辦到的話,嗯,如果他是世界上第一流的橋手,也還不錯。」 -- 班尼多‧加羅素,前義大利藍隊成員 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 218.161.94.175 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1462179906.A.03C.html ※ 編輯: uranusjr (218.161.94.175), 05/02/2016 17:06:07

05/02 19:58, , 1F
謝謝<(_ _)>
05/02 19:58, 1F
文章代碼(AID): #1N9nX20y (Python)
文章代碼(AID): #1N9nX20y (Python)