【发布时间】:2021-03-31 04:59:25
【问题描述】:
我使用python生成html页面。在根文件夹下,我创建了两个文件夹 A 和 B。在 B 中有 b1.html、b2.html 等。在文件夹 A 中,我有 A1.html 和 A2.html 等等。在 A1.html 中,当生成这样的 html 时,我根据 A1.html 中出现的关键字 bx 创建到 bx.html 的链接。
例如:
stringsCounterNames = re.findall('\[([A-Z].*?)\]',line) #find the keywords
stringsCounterNames = list(dict.fromkeys(stringsCounterNames)) #remove duplicates
counter_path="B_Folder"
if (len(stringsCounterName)>0):
for stringCounter in stringsCounterNames:
targetStr=counter_dic[stringCounter]
target = '<a href="' + counter_path + '/' + targetStr + '.html">' + stringCounter + '<a>'
line = re.sub(stringCounter, target, line)
print(line)
在上面的代码中,它会创建一个这样的链接:
<a href="B_Folder/b1.html">stringCounter</a>
所以问题是在浏览 A1.html 时,单击链接,它转到:File:rootFolder/A_Folder/B_Folder/b1.html,所以链接永远不会起作用,因为 A_Folder 和 B_Folder 都是 rootFolder 下的子文件夹。
我可以将 B_Folder 移到 A_Folder 下来解决问题,但我想让文件井井有条。有没有办法轻松解决这个问题?我尝试将“counter_path”更改为“..B_Folder”或“.B_Folder”,但始终无效。我无法为其添加绝对路径,因为它将迁移到其他一些根文件夹。谢谢!
【问题讨论】:
标签: python html directory path