从 python 中的文本树获取文件路径
Get files path from text tree in python
我有下面的文本树,我想获取所有可能的路径。
subdirectory 1
file11
file12
sub-sub-directory 1
file111
file112
subdirectory 2
file 21
sub-sub-directory 21
sub-sub-directory 22
sub-sub-sub-directory 221
file 2211
我希望收到包含所有正确可能路径的控制台输出,例如:
Ex1: Subdirectory 1/Sub-sub-directory 11/file111
Ex2: Subdirectory 1/file12
Ex3: Subdirectory 2/sub-sub-directory 22/sub-sub-sub-directory 221/file 2211
任意组合。任何的想法 ?谢谢
当我运行下面为你的示例文件(这里命名为file.txt
)
import re
from itertools import groupby
re_indent = re.compile(r"^\s*")
def key_func(line):
return len(re_indent.match(line)[0])
with open("file.txt", "r") as file:
for key, parts in groupby(file, key=key_func):
print(f"{key}:", [part.strip() for part in parts])
我基本上得到了(除了格式化):
indent parts
-------- -----------------------------------------------------------
1 ['subdirectory 1']
3 ['file11', 'file12', 'sub-sub-directory 1']
5 ['file111', 'file112']
1 ['subdirectory 2']
3 ['file 21', 'sub-sub-directory 21', 'sub-sub-directory 22']
5 ['sub-sub-sub-directory 221']
7 ['file 2211']
因此,只要文件符合此缩进规则,您就可以尝试以下操作:
import re
from itertools import groupby
re_indent = re.compile(r"^\s*")
def key_func(line):
return len(re_indent.match(line)[0])
last_key = -1
paths = []
current, current_str = [], ""
with open("file.txt", "r") as file:
for key, parts in groupby(file, key=key_func):
if key < last_key:
current = current[:(key - 1) // 2]
current_str = "/".join(current)
for part in parts:
paths.append(f"{current_str}/{part.strip()}".lstrip("/"))
current.append(part.strip())
current_str = paths[-1]
last_key = key
获得
paths = [
'subdirectory 1',
'subdirectory 1/file11',
'subdirectory 1/file12',
'subdirectory 1/sub-sub-directory 1',
'subdirectory 1/sub-sub-directory 1/file111',
'subdirectory 1/sub-sub-directory 1/file112',
'subdirectory 2',
'subdirectory 2/file 21',
'subdirectory 2/sub-sub-directory 21',
'subdirectory 2/sub-sub-directory 22',
'subdirectory 2/sub-sub-directory 22/sub-sub-sub-directory 221',
'subdirectory 2/sub-sub-directory 22/sub-sub-sub-directory 221/file 2211'
]
这似乎是您要找的东西?
我有下面的文本树,我想获取所有可能的路径。
subdirectory 1
file11
file12
sub-sub-directory 1
file111
file112
subdirectory 2
file 21
sub-sub-directory 21
sub-sub-directory 22
sub-sub-sub-directory 221
file 2211
我希望收到包含所有正确可能路径的控制台输出,例如:
Ex1: Subdirectory 1/Sub-sub-directory 11/file111
Ex2: Subdirectory 1/file12
Ex3: Subdirectory 2/sub-sub-directory 22/sub-sub-sub-directory 221/file 2211
任意组合。任何的想法 ?谢谢
当我运行下面为你的示例文件(这里命名为file.txt
)
import re
from itertools import groupby
re_indent = re.compile(r"^\s*")
def key_func(line):
return len(re_indent.match(line)[0])
with open("file.txt", "r") as file:
for key, parts in groupby(file, key=key_func):
print(f"{key}:", [part.strip() for part in parts])
我基本上得到了(除了格式化):
indent parts
-------- -----------------------------------------------------------
1 ['subdirectory 1']
3 ['file11', 'file12', 'sub-sub-directory 1']
5 ['file111', 'file112']
1 ['subdirectory 2']
3 ['file 21', 'sub-sub-directory 21', 'sub-sub-directory 22']
5 ['sub-sub-sub-directory 221']
7 ['file 2211']
因此,只要文件符合此缩进规则,您就可以尝试以下操作:
import re
from itertools import groupby
re_indent = re.compile(r"^\s*")
def key_func(line):
return len(re_indent.match(line)[0])
last_key = -1
paths = []
current, current_str = [], ""
with open("file.txt", "r") as file:
for key, parts in groupby(file, key=key_func):
if key < last_key:
current = current[:(key - 1) // 2]
current_str = "/".join(current)
for part in parts:
paths.append(f"{current_str}/{part.strip()}".lstrip("/"))
current.append(part.strip())
current_str = paths[-1]
last_key = key
获得
paths = [
'subdirectory 1',
'subdirectory 1/file11',
'subdirectory 1/file12',
'subdirectory 1/sub-sub-directory 1',
'subdirectory 1/sub-sub-directory 1/file111',
'subdirectory 1/sub-sub-directory 1/file112',
'subdirectory 2',
'subdirectory 2/file 21',
'subdirectory 2/sub-sub-directory 21',
'subdirectory 2/sub-sub-directory 22',
'subdirectory 2/sub-sub-directory 22/sub-sub-sub-directory 221',
'subdirectory 2/sub-sub-directory 22/sub-sub-sub-directory 221/file 2211'
]
这似乎是您要找的东西?