如何使用Python获取最前面的Windows资源管理器窗口的目录路径?
我的问题是,我正在制作一个程序,该程序将根据用户作为文件浏览器打开的文件夹获取文件的数据。。。我浏览了整个互联网来寻找答案,但似乎以前从来没有人遇到过这个问题。许多答案与项目文件所在的当前工作目录有关如何使用Python获取最前面的Windows资源管理器窗口的目录路径?,python,python-3.x,windows,explorer,Python,Python 3.x,Windows,Explorer,我的问题是,我正在制作一个程序,该程序将根据用户作为文件浏览器打开的文件夹获取文件的数据。。。我浏览了整个互联网来寻找答案,但似乎以前从来没有人遇到过这个问题。许多答案与项目文件所在的当前工作目录有关 因此,我基本上想要用户当前在文件资源管理器中查看的文件夹的路径。我能想象的最好的方法是编写Python软件以使用资源管理器中的上下文菜单。通过快速谷歌搜索,我发现了一个有趣的图书馆: 我认为从另一个进程读取数据是不明智的。这将导致安全问题 import win32gui, time from wi
因此,我基本上想要用户当前在文件资源管理器中查看的文件夹的路径。我能想象的最好的方法是编写Python软件以使用资源管理器中的上下文菜单。通过快速谷歌搜索,我发现了一个有趣的图书馆: 我认为从另一个进程读取数据是不明智的。这将导致安全问题
import win32gui, time
from win32con import PAGE_READWRITE, MEM_COMMIT, MEM_RESERVE, MEM_RELEASE, PROCESS_ALL_ACCESS, WM_GETTEXTLENGTH, WM_GETTEXT
from commctrl import LVS_OWNERDATA, LVM_GETITEMCOUNT, LVM_GETNEXTITEM, LVNI_SELECTED
import os
import struct
import ctypes
import win32api
import datetime
import win32com.client as win32
import win32ui
import psutil
import subprocess
import time
import urllib.parse
clsid = '{9BA05972-F6A8-11CF-A442-00A0C90A8F39}' #Valid for IE as well!
def getEditText(hwnd):
# api returns 16 bit characters so buffer needs 1 more char for null and twice the num of chars
buf_size = (win32gui.SendMessage(hwnd, WM_GETTEXTLENGTH, 0, 0) +1 ) * 2
target_buff = ctypes.create_string_buffer(buf_size)
win32gui.SendMessage(hwnd, WM_GETTEXT, buf_size, ctypes.addressof(target_buff))
return target_buff.raw.decode('utf16')[:-1]# remove the null char on the end
def _normaliseText(controlText):
'''Remove '&' characters, and lower case.
Useful for matching control text.'''
return controlText.lower().replace('&', '')
def _windowEnumerationHandler(hwnd, resultList):
'''Pass to win32gui.EnumWindows() to generate list of window handle,
window text, window class tuples.'''
resultList.append((hwnd, win32gui.GetWindowText(hwnd), win32gui.GetClassName(hwnd)))
def searchChildWindows(currentHwnd,
wantedText=None,
wantedClass=None,
selectionFunction=None):
results = []
childWindows = []
try:
win32gui.EnumChildWindows(currentHwnd,
_windowEnumerationHandler,
childWindows)
except win32gui.error:
# This seems to mean that the control *cannot* have child windows,
# i.e. not a container.
return
for childHwnd, windowText, windowClass in childWindows:
descendentMatchingHwnds = searchChildWindows(childHwnd)
if descendentMatchingHwnds:
results += descendentMatchingHwnds
if wantedText and \
not _normaliseText(wantedText) in _normaliseText(windowText):
continue
if wantedClass and \
not windowClass == wantedClass:
continue
if selectionFunction and \
not selectionFunction(childHwnd):
continue
results.append(childHwnd)
return results
def explorer_fileselection():
global clsid
address_1=""
files = []
shellwindows = win32.Dispatch(clsid)
w=win32gui
window = w.GetForegroundWindow()
#print("window: %s" % window)
if (window != 0):
if (w.GetClassName(window) == 'CabinetWClass'): # the main explorer window
#print("class: %s" % w.GetClassName(window))
#print("text: %s " %w.GetWindowText(window))
children = list(set(searchChildWindows(window)))
addr_edit = None
file_view = None
for child in children:
if (w.GetClassName(child) == 'WorkerW'): # the address bar
addr_children = list(set(searchChildWindows(child)))
for addr_child in addr_children:
if (w.GetClassName(addr_child) == 'ReBarWindow32'):
addr_edit = addr_child
addr_children = list(set(searchChildWindows(child)))
for addr_child in addr_children:
if (w.GetClassName(addr_child) == 'Address Band Root'):
addr_edit = addr_child
addr_children = list(set(searchChildWindows(child)))
for addr_child in addr_children:
if (w.GetClassName(addr_child) == 'msctls_progress32'):
addr_edit = addr_child
addr_children = list(set(searchChildWindows(child)))
for addr_child in addr_children:
if (w.GetClassName(addr_child) == 'Breadcrumb Parent'):
addr_edit = addr_child
addr_children = list(set(searchChildWindows(child)))
for addr_child in addr_children:
if (w.GetClassName(addr_child) == 'ToolbarWindow32'):
text=getEditText(addr_child)
if "\\" in text:
address_1=getEditText(addr_child)[text.index(" ")+1:]
print("Address --> "+address_1)
for window in range(shellwindows.Count):
window_URL = urllib.parse.unquote(shellwindows[window].LocationURL,encoding='ISO 8859-1')
window_dir = window_URL.split("///")[1].replace("/", "\\")
print("Directory --> "+window_dir)
if window_dir==address_1:
selected_files = shellwindows[window].Document.SelectedItems()
for file in range(selected_files.Count):
files.append(selected_files.Item(file).Path)
print("Files --> "+str(files))
while True:
try:
explorer_fileselection()
except Exception:
print("No Path Found!")
time.sleep(1)
嗯,我的解决方案很复杂,但我为你找到了这个。我希望它能有所帮助。什么是文件资源管理器?你的意思是包括像ranger这样的终端文件浏览器还是一个特定的文件浏览器?如果用户打开5个不同的文件夹,其中包含一个文件浏览器的5个实例,该怎么办?@ThomasSablik可能是一个文件浏览器内置的窗口,我想,这是我唯一能想到的同名窗口。@jupiterbjy在这个问题上看不到窗口。一个普通用户每天使用的特定窗口。。。就像我的电脑在windows中一样。我想要用户当前在我的电脑中所在文件夹的路径。例如(D:\Games),这是否回答了您的问题?“我认为没有明智的方法从另一个进程读取数据。这会导致安全问题。”它被称为API。许多程序为这种交流提供了接口:@ThomasSablik:谢谢你提供的信息。我不知道Windows资源管理器提供API。我在想,这个场景试图从一个不提供API的进程或API不提供的数据中读取数据。例如,我希望没有人可以从我的banksoftware.exe进程读取数据。在Windows上,您可以。这就是大多数游戏作弊软件的工作原理。他们读取游戏过程的记忆,并试图解释这些值,以便以后更改它们。在其他操作系统上,这是困难的或不可能的。