Html 使用Python单击HTML页面上的某个链接时如何打开Windows文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11806853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 02:04:58  来源:igfitidea点击:

How to open a windows folder when clicking on some link on a HTML page using Python

htmlwindowsinternet-explorerfirefoxgoogle-chrome

提问by jags

I am writing following program :

我正在编写以下程序:

***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>')***

What I want to do is if I click First Link on a browser, I should be able to open the folder having path as "Filepath". os.startfile works perfect for opening a folder but I don't know how to implement this inside some link. Thanks.

我想要做的是,如果我在浏览器上单击第一个链接,我应该能够打开路径为“文件路径”的文件夹。os.startfile 非常适合打开文件夹,但我不知道如何在某些链接中实现它。谢谢。

回答by Vladimir

Try to use URI with file:scheme like file:///C:/TestData/openfolder.htmlin your html:

尝试将 URI 与您的 html 中的file:方案一起使用file:///C:/TestData/openfolder.html

<a href="file:///C:/TestData/openfolder.html">Link to test data</a>

Here is article on using file URIs in Windows.

这是关于在 Windows 中使用文件 URI 的文章

UPD (extraction from comments):Each browser has its own way to handle such urls. At least Internet Explorer 8 under Windows 7 opens links in Windows Explorer as was required by jags.

UPD(从评论中提取):每个浏览器都有自己的方式来处理此类 url。至少在 Windows 7 下的 Internet Explorer 8 会按照 jags 的要求在 Windows 资源管理器中打开链接。

Finally, for dynamic pages the web serveris required. If one is needed take a look at discussion on creating simple web services using python.

最后,对于动态页面,需要Web 服务器。如果需要,请查看有关使用 python 创建简单 Web 服务的讨论

回答by aychedee

You can't. Clicking a link to a file in a browser will not launch the application associated with that file type on the OS. You can apparently do some funky stuff with JavaScript to launch particular filetypes with particular applications (see here: http://forums.devshed.com/asp-programming-51/launching-ms-word-to-open-file-from-a-hyperlink-55714.html) but apart from that the web browser is not the file browser.

你不能。在浏览器中单击文件链接不会在操作系统上启动与该文件类型关联的应用程序。您显然可以用 JavaScript 做一些时髦的事情来启动特定应用程序的特定文件类型(请参阅此处:http: //forums.devshed.com/asp-programming-51/launching-ms-word-to-open-file-from- a-hyperlink-55714.html) 但除此之外,网络浏览器不是文件浏览器。

回答by Alain

<a href="FOLDER_PATH" target="_explorer.exe">Link Text</a>

Replace FOLDER_PATHwith the path of the folder you want to open in explorer.

替换FOLDER_PATH为您要在资源管理器中打开的文件夹的路径。

回答by Charles Young

Alain's answer works.

阿兰的回答有效。

<'a href="FOLDER_PATH" target="_explorer.exe">Link Text<'/a>

<'a href="FOLDER_PATH" target="_explorer.exe">链接文字</'/a>

I removed the tick marks at the beginning and end, and found that it works in

我删除了开头和结尾的刻度线,发现它在

  • Internet Explorer - opens a Windows Explorer window

  • Firefox (Windows and Linux), but opens a new tab - same as target="_blank"

  • Chrome - opens a new tab like Firefox

  • Internet Explorer - 打开 Windows 资源管理器窗口

  • Firefox(Windows 和 Linux),但会打开一个新选项卡 - 与 target="_blank" 相同

  • Chrome - 打开一个像 Firefox 一样的新标签

I also noticed that / and \ (forward and backward slashes) are equal in html links - they can even be mixed.

我还注意到 / 和 \(正斜杠和反斜杠)在 html 链接中是相等的——它们甚至可以混合使用。