看板 Python 關於我們 聯絡資訊
※ 引述《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())
doomleika: 請問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
doomleika: 謝謝<(_ _)> 05/02 19:58