2012-11-09 100 views
6

我想打開Windows資源管理器並選擇特定文件。 這是API:explorer /select,"PATH"。因此,導致下面的代碼(使用python 2.7):啓動GUI進程而不會產生黑色外殼窗口

import os 

PATH = r"G:\testing\189.mp3" 
cmd = r'explorer /select,"%s"' % PATH 

os.system(cmd) 

的代碼工作正常,但是當我切換到非殼模式(pythonw),之前的資源管理器是出現了片刻黑色外殼窗口推出。

這是與os.system預計。我創建了以下函數來啓動進程而不產生窗口:

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 

這對沒有GUI的shell可執行文件工作正常。但它不會啓動explorer.exe

如何在不產生黑窗的情況下啓動過程?

+1

令人吃驚:我第C的WinExec與和ShellExec試圖/ C++代碼,它給我相同的行爲。 – lucasg

回答

3

這似乎不可能。但是它可以從win32api訪問。我使用的代碼中發現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']) 
+0

你可以看看這個問題嗎?謝謝!!! http://stackoverflow.com/questions/19851113/pywin32-programming-error-on-win7-with-shell-shgetdesktopfolder-desktop-bindtoob – iMath