作者uranusjr (←这人是超级笨蛋)
看板Python
标题Re: [问题] 用file open抓TXT开的问题
时间Mon May 2 17:05:04 2016
※ 引述《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())
1F:→ 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://webptt.com/cn.aspx?n=bbs/Python/M.1462179906.A.03C.html
※ 编辑: uranusjr (218.161.94.175), 05/02/2016 17:06:07
2F:推 doomleika: 谢谢<(_ _)> 05/02 19:58