python资源管理器选择文件_Python:在资源管理器中获取选定文件的列表(windows7)...
我知道現在在這里發布答案有點晚了,但我幾個月前嘗試過Olav的解決方案,但它沒有完全起作用:工作目錄是腳本的工作目錄,所以我不得不刪除if條件才能使其工作,但它選擇了所有Windows資源管理器窗口中的所有文件(我也希望這樣做,所以這部分對我有效)。但現在我回來繼續我的項目(一個助理),我發現我真的需要這個工作,所以我考慮這個想法(這不是很難想到,但我花了幾個月的時間…)。我不知道這個答案是否對其他人有用,但對我來說,它并不完全有效,所以我想我可以改進它,并在這里發布我的解決方案。這段代碼是這個答案的混合體(我在同一個腳本中也使用過,但從未想過讓它們一起工作):https://stackoverflow.com/a/43892579/8228163(至少在Windows7下可以工作)和Olav的答案以及對我有用的結果——腳本只在當前的Windows資源管理器窗口中檢測文件。我想從Vista(也許,我不知道它的年齡超過7歲)到10歲,但我不完全確定。另一個答案是使用XP。當我在Windows10上啟動這個腳本時,我認為它是有效的,但是我已經沒有10個了,所以我不能確定(我又用了7,所以對于7來說這是有效的)。在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:
explorer_fileselection()
time.sleep(1)
這將查找活動的Windows資源管理器窗口,獲取該窗口的地址,然后將該地址用于Olav的答案,以檢查該地址是否等于在Windows資源管理器中打開的地址之一,從而從活動窗口獲取文件。順便說一句,因為這個腳本是兩個答案的修改副本,所以它有來自這些答案的限制。所以,就像Olav的回答“編輯:還不起作用,至少在使用上下文菜單時是這樣的”,那么這可能也不起作用,因為它是相同的代碼-只是工作目錄不同(雖然,我不知道他說的是什么意思,但就我測試的結果來看,它起作用了)。就像jameskent的答案一樣,這不適用于桌面,只適用于使用windows資源管理器打開的窗口。
編碼=“ISO 8859-1”是因為我是葡萄牙語,但可以更改,只需確保兩個目錄都相等而不必使用%?否則就沒用了!在
因為這個問題只有近5年的時間,OP可能不再需要它了,但我需要它,卻沒有它在任何地方,所以我想我可以把這個貼在這里,也許可以幫助其他想這樣做的人。腳本中的代碼可用于了解當前Windows資源管理器窗口上的文件,并在XP以上的Windows上獲取當前的Windows資源管理器窗口路徑(對Vista不確定)。對于XP,請參閱原始答案(https://stackoverflow.com/a/43892579/8228163),要從所有Windows資源管理器窗口獲取文件,只需從Olav的答案中刪除if條件。在
感謝Olav和James Kent給出的答案,因為我會花更多的時間來嘗試如何做到這一點(我是一個Python/任何語言begginner—只需編寫一年代碼,所以需要花費大量時間,也許我必須將其與另一個語言混合使用)。再次感謝你們,感謝你們的行動,感謝你們在正確的時間問了問題,讓合適的人來回答!(因為Olav在鏈接上引用的源不再存在)。在
希望這有幫助!干杯!在
總結
以上是生活随笔為你收集整理的python资源管理器选择文件_Python:在资源管理器中获取选定文件的列表(windows7)...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: word公式编辑器_【Word技巧】wo
- 下一篇: mr图像翻转的原因_MRI图像常见问题及