python - 如何在python中创建一棵树?
问题描述
我有一个包含 100 个 html 文件的文件夹,我正在尝试创建一个名称为 page1 - page100 的树。每个页面都有一个超链接,可以打开另一个页面。我试图让它到达程序读取根节点(page1.html)的位置,读取其超链接并根据这些链接创建子节点,然后对其余节点重复此操作,直到树完成。使用超链接解决此问题的最佳方法是什么?到目前为止,这是我的代码。
import os
from math import*
from os.path import isfile, join
entries = os.listdir("C:/Users/deonh/Downloads/intranets/intranet1") #This reads the directory
onlyfiles = [f for f in entries if isfile(join("C:/Users/deonh/Downloads/intranets/intranet1", f))] #This took all the webpages in the directory and put them into a list.
print(onlyfiles)
web = open("C:/Users/deonh/Downloads/intranets/intranet1" + "/" + onlyfiles[0]) # This will tell us if the webpage is readable or not
print(web.readable()) # This tells if the file is readable
print(web.readlines()) #This reads the content of the file
web.close()
解决方案
您可以使用 os.walk 遍历所有子目录:
import os
path = 'C:/Users/deonh/Downloads/intranets/intranet1'
entries = []
onlyfiles = []
for directory, _, files in os.walk(path): # second value would be subdirectries but we don't need them because we iterate over all directories
entries.append(directory)
for file in files:
onlyfiles.append('{}/{}'.format(directory, file)) #
if os.access(onlyfiles[0], os.R_OK): # This will tell us if the webpage is readable or not
with open(onlyfiles[0]) as file: # file is closing automatically
print(file.readlines())
print(onlyfiles[0], os.R_OK) # This tells if the file is readable
print(onlyfiles) # This reads the content of the file
请询问是否有不清楚的地方。