如何在python中创建树?

2024-04-25 07:40:40 发布

您现在位置: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()

Tags: theimportwebif节点osdownloadsthis
1条回答
网友
1楼 · 发布于 2024-04-25 07:40:40

你可以用os.步行迭代所有子目录:

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

如果有不清楚的地方,请询问。你知道吗

相关问题 更多 >

    热门问题