我正在编写以下程序:
***import os
filepath=r'C:\TestData\openfolder.html'
abc=open(filepath,'w')
abc.writelines('<html><head></head><body>')
abc.writelines('<a href="os.startfile(filepath)">First Link</a>\n')
abc.writelines('</body></html>')***
我想要做的是,如果我在浏览器上单击第一个链接,我应该能够打开路径为“文件路径”的文件夹。 os.startfile 非常适合打开文件夹,但我不知道如何在某些链接中实现它。 谢谢。
最佳答案
尝试将 URI 与 file:
方案一起使用,如 file:///C:/TestData/openfolder.html
在您的 html 中:
<a href="file:///C:/TestData/openfolder.html">Link to test data</a>
这里是 article on using file URIs in Windows .
UPD(从评论中提取):每个浏览器都有自己的方式来处理此类 url。至少 Windows 7 下的 Internet Explorer 8 会按照 jags 的要求在 Windows 资源管理器中打开链接。
最后,对于动态页面 web server是必须的。如果需要,请查看 discussion on creating simple web services using python .
关于html - 使用 Python 单击 HTML 页面上的某些链接时如何打开 Windows 文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11806853/