我知道如何使用python通过以下方式在资源管理器中打开文件夹:
subprocess.Popen(r'explorer /select,"C:\path\of\folder"')
但是我不知道如果它已经在资源管理器中“打开”,我怎么防止我的程序打开文件夹。有没有办法在Python中(或通过VBA脚本)?
答案 0 :(得分:1)
我认为您的问题主要是由于用户重新打开或使用您的应用重新点击。
部分执行此操作的一种方法可能是:
您可以在变量中存储您打开的资源管理器的pid(进程-id):subprocess.Popen(r'explorer /select,"C:\path\of\folder"')
然后当试图再次打开时,如果设置了pid。检查进程是否仍处于活动状态。如果是的话,那就不要打开它,也许找到一种方法来关注被打开的寡妇。
答案 1 :(得分:0)
这是一个有趣的线程,我找到了一个列出打开文件夹的工作解决方案与VBS脚本,但我不知道如何使用VBS,因此我无法解决identifier excepted
错误并使其正常工作..
代码本身是:
Imports System.Runtime.InteropServices
导入System.Text
Public Class Form1 Private Const WM_GETTEXT As Integer =& HD Private Const WM_GETTEXTLENGTH As Integer =& HE
<DllImport("user32.dll", EntryPoint:="FindWindowExW")> _
Private Shared Function FindWindowExW(ByVal hwndParent As System.IntPtr, ByVal hwndChildAfter As System.IntPtr, <InAttribute(), MarshalAs(UnmanagedType.LPTStr)> ByVal lpszClass As String, <InAttribute(), MarshalAs(UnmanagedType.LPTStr)> ByVal lpszWindow As String) As System.IntPtr
End Function
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As Integer, ByVal lParam As StringBuilder) As Integer
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ListBox1.Items.Clear()
Dim hWinList As New List(Of IntPtr)
'Get Each Explorer Windows Handle
Dim hWnd As IntPtr = FindWindowExW(IntPtr.Zero, IntPtr.Zero, "CabinetWClass", Nothing)
While Not hWnd.Equals(IntPtr.Zero)
hWinList.Add(hWnd)
hWnd = FindWindowExW(IntPtr.Zero, hWnd, "CabinetWClass", Nothing)
End While
'Loop threw each explorer window in the list and get the text from the Address combobox
If hWinList.Count > 0 Then
For Each hChld As IntPtr In hWinList
Dim hChild1 As IntPtr = FindWindowExW(hChld, IntPtr.Zero, "WorkerW", Nothing)
Dim hChild2 As IntPtr = FindWindowExW(hChild1, IntPtr.Zero, "ReBarWindow32", Nothing)
Dim hChild3 As IntPtr = FindWindowExW(hChild2, IntPtr.Zero, "ComboBoxEx32", Nothing)
Dim len As Integer = SendMessage(hChild3, WM_GETTEXTLENGTH, 0, Nothing)
Dim sb As New StringBuilder(len + 1)
SendMessage(hChild3, WM_GETTEXT, len + 1, sb)
ListBox1.Items.Add(sb.ToString)
Next
End If
End Sub
结束班
答案 2 :(得分:-4)
我不确定你的目的是什么,但也许这样的事情会有所帮助:
import os
for root, dirs, files in os.walk(Folder_Root, topdown=False):
for name in dirs:
full_path = os.path.join(root, name)
#use Popen to open the folder here
读取Folder_Root下的所有目录,然后用Popen打开每个目录。每个文件夹只打开一次。只需将Folder_Root替换为实际路径即可。