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

时间:2022-06-02 05:52:37

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.

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

4 个解决方案

#1


7  

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

尝试使用URI和file: scheme,比如file:///C:/TestData/openfolder。html在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。至少ie 8在Windows 7下打开了Windows Explorer中的链接,这是jags要求的。

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

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

#2


3  

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),但除此之外,web浏览器不是文件浏览器。

#3


2  

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

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

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

#4


-1  

Alain's answer works.

阿兰的回答。

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

< a href = " FOLDER_PATH "目标=”_explorer。exe“>链接文本< / >

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

我去掉了开头和结尾的记号,发现它起作用了

  • Internet Explorer - opens a Windows Explorer window

    打开一个Windows资源管理器窗口

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

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

  • Chrome - opens a new tab like Firefox

    Chrome -打开一个新的标签,比如Firefox

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

我还注意到/和\(向前和向后斜杠)在html链接中是相等的——它们甚至可以混合在一起。

#1


7  

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

尝试使用URI和file: scheme,比如file:///C:/TestData/openfolder。html在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。至少ie 8在Windows 7下打开了Windows Explorer中的链接,这是jags要求的。

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

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

#2


3  

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),但除此之外,web浏览器不是文件浏览器。

#3


2  

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

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

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

#4


-1  

Alain's answer works.

阿兰的回答。

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

< a href = " FOLDER_PATH "目标=”_explorer。exe“>链接文本< / >

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

我去掉了开头和结尾的记号,发现它起作用了

  • Internet Explorer - opens a Windows Explorer window

    打开一个Windows资源管理器窗口

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

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

  • Chrome - opens a new tab like Firefox

    Chrome -打开一个新的标签,比如Firefox

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

我还注意到/和\(向前和向后斜杠)在html链接中是相等的——它们甚至可以混合在一起。