6

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.

jags
  • 527
  • 3
  • 6
  • 15
  • 1
    This question does not relay to python. Also, it's a good idea to explicitly specify your OS as solution may differs even on various Windows versions. – Vladimir Aug 04 '12 at 07:53

4 Answers4

8

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.

Community
  • 1
  • 1
Vladimir
  • 9,913
  • 4
  • 26
  • 37
  • Thanks for replying back. Actually I don't want to open folder in a browser. I want to open it like the way os.startfile(filepath) open it. And, my filepath changes randomnly. So, I can't hard code and put a specific value inside href. My filepath inside href changes for different links and is not fixed. Can you please tell me some solution regarding this. I am using windows OS. – jags Aug 04 '12 at 08:36
  • Well, please clarify the usage scenario of what are you trying to build. You want to have some dynamic web-page with links to your local folders, right? And then you are going to open that links and show them in explorer? – Vladimir Aug 04 '12 at 09:32
  • Moreover, I've just checked Internet Explorer 8 under freshly installed Windows 7 Home Basic and it opens file: URLs in Windows Explorer not in itself. – Vladimir Aug 04 '12 at 09:40
  • Thank you very much Dair. Yes, it works in Internet Explorer. I am left with only 1 problem: the link inside href is not static. It is different for different files. So, if I specify newpath=r'file:\\\C:\New_folder\Engineering' and If I want to use this inside href like Link to test data, this will not be able to access newpath data. If you can please help me in providing dyanamic link (which changes) inside href , that will be great. – jags Aug 05 '12 at 05:30
  • For dynamic page generation you need to use web-service. Here, for example, a discussion on simple python web-server implementation: http://stackoverflow.com/questions/415192/best-way-to-create-a-simple-python-web-service . – Vladimir Aug 05 '12 at 12:57
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.

aychedee
  • 24,871
  • 8
  • 79
  • 83
3
<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.

stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
Alain
  • 31
  • 1
0

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.