Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Python (http://www.programmingforums.org/forum43.html)
-   -   Show File in Window (http://www.programmingforums.org/showthread.php?t=10658)

titaniumdecoy Jul 7th, 2006 7:39 PM

Show File in Window
 
I have the path to a file, say C:\path\to\file.txt. I need a way to open the folder where that file (file.txt) resides, and select it.

Since I am on a Windows machine, I can use the webbrowser module to load a path:

:

import webbrowser

webbrowser.open(r"C:\path\to\file.txt")

However, this does not select the file. (I do not want to open it, just to select it.)

Thanks in advance. I'm not even sure this is possible in Python.

Sane Jul 7th, 2006 7:50 PM

Oh, err that sounds really strange. I couldn't see why there'd be support for something like that. If someone knows something in the win32api to prove me wrong, that'd be cool. :)

Otherwise, I'd think it would call for something really obscure. Like synthesizing a keystroke using win32api.keybd_event. First off, have you ever used an application that does what you are trying to do? That might be a helpful clue as to if it is even possible.

titaniumdecoy Jul 7th, 2006 8:11 PM

I'm not sure if it's the same in Windows, but on my Mac, when I download a file in my browser, I can go to the Download Manager window and click "Show File in Finder," and it will open a window with the file I just downloaded, selected. I'm trying to do something similar, although it's probably not going to be possible...

Arevos Jul 8th, 2006 7:36 AM

LaunchServices looks promising. Mac only, though.

Cerulean Jul 8th, 2006 10:44 AM

That does sound pretty obscure. If i'm not mistaken the explorer uses a HTML rendering widget to display the folders and stuff? If you could find a way to get a reference to that rendering widget you could find a way to call some JavaScript in it and focus the folder. It'd be a pretty involved process though.

Dietrich Jul 8th, 2006 3:07 PM

On the InternetExplorer you can click on File/Properties.

Sane Jul 8th, 2006 3:43 PM

Huh... Dietrich? What does that have to do with anything at all...? :S

niteice Jul 8th, 2006 6:14 PM

Perhaps you want to execute "explorer.exe /select, C:\path\to\file.txt" ?

Note the comma, as per MSKB.

I dunno how to do it in python, but that should get you what you want.

Sane Jul 8th, 2006 6:22 PM

Oh wow! That's amazing, it works. :p So easy... so the code would then be...


:


import os

path = 'c:\\path\\file.txt'

os.system('explorer.exe /select, %s'%path)


Edit: I'll post the code for if the path has spaces in it, when I get back from dinner.

Post Edit: Oh! Nvm. It works with spaces already. :o

titaniumdecoy Jul 8th, 2006 8:52 PM

Wow, thanks... I didn't think it was going to be possible! I'll try this out as soon as I get to work on Monday.


All times are GMT -5. The time now is 12:55 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC