는 여기에 지금까지있어 무엇 :디렉토리 트리를 HTML로 출력하는 방법은 무엇입니까?
project_dir = '/my/project/dir'
project_depth = len(project_dir.split(os.path.sep))
xml_files = []
for dirpath, dirnames, filenames in os.walk(project_dir):
for filename in fnmatch.filter(filenames, '*.xml'):
dirs = dirpath.split(os.path.sep)[project_depth:]
print(dirs)
xml_files.append(os.path.join(dirpath,filename))
기본적으로 내가 HTML 트리 같은 모든 XML 파일을 내 프로젝트의 디렉토리 구조를 뱉어되어 수행 할 작업 (사용 <ul>
). 이 방법으로 모든 파일을 가져올 수는 있지만 트리로 구성하는 방법을 파악할 수 없습니다.
이 os.walk
이 작동하는 방식으로 나는 더 깊은 레벨에 갔을 때 또는 내가 여전히 같은 디렉토리를 통과하고 있는지 알 수 없습니다.
for dirpath, dirnames, filenames in os.walk(project_dir):
xml_files = fnmatch.filter(filenames, '*.xml')
if len(xml_files) > 0:
out.write('<li>{0}<ul>'.format(dirpath))
for f in xml_files:
out.write('<li>{0}</li>'.format(f))
out.write('</ul></li>')
out.write('</ul>')
이 나에게 디렉토리의 목록과 그 아래에있는 모든 파일을 제공하지만, 난 여전히 너무 중첩 것 있도록 디렉토리 경로를 분할하는 방법을 알아낼 수 없습니다.