Start the GUI process without a black shell window

I want to open Windows Explorer and select a specific file. This is the API: explorer /select,"PATH" . Therefore, the result is the following code (using python 2.7):

 import os PATH = r"G:\testing\189.mp3" cmd = r'explorer /select,"%s"' % PATH os.system(cmd) 

The code works fine, but when I switch to pythonw mode (with pythonw ), a black shell window appears a moment before the pythonw starts.

This can be expected with os.system . I created the following function to start processes without a window appearing:

 import subprocess, _subprocess def launch_without_console(cmd): "Function launches a process without spawning a window. Returns subprocess.Popen object." suinfo = subprocess.STARTUPINFO() suinfo.dwFlags |= _subprocess.STARTF_USESHOWWINDOW p = subprocess.Popen(cmd, -1, stdout=subprocess.PIPE, stderr=subprocess.PIPE, startupinfo=suinfo) return p 

This works great for shell executables without a GUI. However, it does not start explorer.exe .

How to start a process without a black window before?

+6
source share
1 answer

This is not possible. However, access to it is possible from win32api . I used the code found here :

 from win32com.shell import shell def launch_file_explorer(path, files): ''' Given a absolute base path and names of its children (no path), open up one File Explorer window with all the child files selected ''' folder_pidl = shell.SHILCreateFromPath(path,0)[0] desktop = shell.SHGetDesktopFolder() shell_folder = desktop.BindToObject(folder_pidl, None,shell.IID_IShellFolder) name_to_item_mapping = dict([(desktop.GetDisplayNameOf(item, 0), item) for item in shell_folder]) to_show = [] for file in files: if name_to_item_mapping.has_key(file): to_show.append(name_to_item_mapping[file]) # else: # raise Exception('File: "%s" not found in "%s"' % (file, path)) shell.SHOpenFolderAndSelectItems(folder_pidl, to_show, 0) launch_file_explorer(r'G:\testing', ['189.mp3']) 
+3
source

All Articles