[英]How to use Windows File Explorer to select and return a directory using Python?
我有一個腳本可以從遠程網絡節點中取出文件並將它們保存到遠程驅動器。 現在,我的腳本需要一個硬編碼的目的地作為它的位置,例如:
dest_path_cfg = f"G:\\path\\to\\my\\folder"
我想更新這個腳本,以便運行它的用戶可以 select 他們想通過文件資源管理器保存文件的文件夾。 我已經看到了如何使用:
import subprocess
subprocess.Popen(r'explorer /select,"C:\path\of\folder\file"')
打開文件資源管理器 window 到給定目錄,但我不知道如何使用子進程允許將用戶的選擇返回到 python 腳本,以便以后可以對其進行操作。
現在我只關注 Windows 的兼容性,但我想在未來增加 Mac/Linux 的靈活性。
您可以使用 Python 的 Tkinter 模塊,其中包含 function 文件filedialog.askdirectory()
。 這將打開標准 Windows 文件夾選擇對話框。
import tkinter
from tkinter import filedialog
tkinter.Tk().withdraw() # prevents an empty tkinter window from appearing
folder_path = filedialog.askdirectory()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.