How to open a Windows folder when clicking on any link on an HTML page using Python

I am writing the 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 the First link in the browser, I have to open a folder with a path like "File path". os.startfile works fine for opening a folder, but I don't know how to implement this inside some link. Thanks.

+4
source share
4 answers

Try using a URI with a file: schema like file:///C:/TestData/openfolder.html in html:

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

Here is an article about using URI files in Windows .

UPD (extract from comments): Each browser has its own way of handling such URLs. At least Internet Explorer 8 under Windows 7 opens links in Windows Explorer, as required by jags.

Finally, dynamic pages require a web server . If necessary, take a look at the discussion of creating simple web services using python .

+7
source

You can not. Clicking the file link in the browser will not launch the application associated with this file type in the OS. Apparently you can make some funky with JavaScript to run certain file types with specific applications (see here: http://forums.devshed.com/asp-programming-51/launching-ms-word-to-open- file-from-a-hyperlink-55714.html ), but other than that, the web browser is not a file browser.

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

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

+1
source

Work with Alain answers.

<'a href = "FOLDER_PATH" target = "_ explorer.exe"> Link text <' / a>

I removed the labels at the beginning and at the end and found that it works in

  • Internet Explorer - Opens Windows Explorer

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

  • Chrome - opens a new tab, for example Firefox

I also noticed that / and \ (forward and backslashes) are equal in html links - you can even mix them.

-1
source

All Articles