How to open a windows folder when clicking on some

2019-02-14 08:52发布

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.

4条回答
Luminary・发光体
2楼-- · 2019-02-14 09:23

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

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

Here is article on using file URIs in Windows.

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.

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.

查看更多
混吃等死
3楼-- · 2019-02-14 09:31
<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.

查看更多
做个烂人
4楼-- · 2019-02-14 09:43

Alain's answer works.

<'a href="FOLDER_PATH" target="_explorer.exe">Link Text<'/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

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

查看更多
做自己的国王
5楼-- · 2019-02-14 09:44

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.

查看更多
登录 后发表回答