/ /黒いシェルウィンドウを生成せずにGUIプロセスを起動する - python、subprocess、explorer

ブラックシェルウィンドウを生成せずにGUIプロセスを起動する - python、subprocess、explorer

Windowsエクスプローラを開き、特定のファイルを選択したいです。 これがAPIです。 explorer /select,"PATH"。したがって、次のようなコードになります(python 2.7を使用)。

import os

PATH = r"G:testing189.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のない​​シェル実行可能ファイルに対してはうまく働きます。しかしそれは発売されなかった explorer.exe.

黒いウィンドウを開かずにプロセスを起動する方法を教えてください。

回答:

回答№1の場合は3

それは可能ではないようです。しかしそれはからアクセスすることができます win32api。見つかったコードを使用しました ここに

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"])