我知道如何在資源管理器與Python通過打開文件夾:在資源管理器中打開一個文件夾,如果尚未打開
subprocess.Popen(r'explorer /select,"C:\path\of\folder"')
但我不知道如何阻止我的程序從打開如果文件夾它已經在瀏覽器中「打開」了。有沒有辦法在Python中做到這一點(或者通過VBA腳本)?
我知道如何在資源管理器與Python通過打開文件夾:在資源管理器中打開一個文件夾,如果尚未打開
subprocess.Popen(r'explorer /select,"C:\path\of\folder"')
但我不知道如何阻止我的程序從打開如果文件夾它已經在瀏覽器中「打開」了。有沒有辦法在Python中做到這一點(或者通過VBA腳本)?
我想你的問題主要是由於用戶重新打開或重新點擊使用你的應用程序。你可以在瀏覽器中你開了一個變量的PID(進程-id)存儲
::部分做的也可能是
一種方式subprocess.Popen(r'explorer /select,"C:\path\of\folder"')
然後,當試圖再次打開,如果一個pid被設置。檢查過程是否仍然有效。如果是的話,那麼不要打開它,也許找到一種方法來專注於打開的寡婦。
偉大的想法沒有實現它的每一個解決方案可能是有用的。如果用戶在程序之前打開文件夾,那麼仍然存在問題,然後該程序將不具有該pid。 – 83101118101110
是的,這是限制。我不知道是否有辦法做得更好。 –
我不知道你要什麼,但也許這樣的事情會幫助:
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替換爲實際路徑即可。
我想要做的是防止我的程序打開一個文件夾,如果它已被用戶在資源管理器中打開,不打開文件夾的每個子目錄。 – 83101118101110
這個線程可能會有所幫助:https://stackoverflow.com/questions/12726218/name-of-files-opened-by-a-process-in-window – theObserver
使用psutil似乎是解決問題的好辦法,它擅長查找運行的二進制文件,但由於它們不是進程,因此打開文件夾比較困難。解決方案可能是尋求進程「explorer.exe」,但我沒有找到一個正確的方法來做到這一點。 – 83101118101110
這裏有一個有趣的線程,我發現其中一個工作解決方案列出打開的文件夾與VBS腳本找到,但我不知道如何使用VBS,因此我無法解決的錯誤和identifier excepted
它正常工作..
代碼本身是:
Imports System.Runtime.InteropServices
進口System.Text
公共類Form1中 私人常量WM_GETTEXT爲整數= & HD 私人常量WM_GETTEXTLENGTH爲整數= & 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
末級
很好的問題,但並沒有真正涉及到蟒蛇 –
我想實現它在python,但它是一個相當普遍的問題的確,即使使用python如果用戶試圖在程序已經打開,打開文件夾解決問題 – 83101118101110