use*_*612 5 python directory tkinter python-3.x
我正在尝试选择多个文件夹。我需要等价于askopenfilenames()for的目录,但仅askdirectory()存在,仅允许选择一个文件夹。
以前,我找到了一个自定义脚本来为Matlab(uigetdir)执行此操作。用Python可以做到这一点吗?
我需要一次在大约50个文件夹中批处理文件,一个接一个地选择它们是不现实的。
而且,我不是程序员,只是尝试处理我的地球物理数据,就不能像我在其他地方看到的那样“自己编写”。本来以为这样的基本东西会包含在基本功能中。
OP 要求使用 Tkinter 提供解决方案,但该解决方案不可用,但可以使用 wxPython-Phoenix 提供解决方案
####### Retrieve a list of directories with wxPython-Phoenix - tested on python3.5
### installation instruction for wxPython-Phoenix : https://wiki.wxpython.org/How%20to%20install%20wxPython#Installing_wxPython-Phoenix_using_pip
### modified from : https://wxpython.org/Phoenix/docs/html/wx.lib.agw.multidirdialog.html
import os
import wx
import wx.lib.agw.multidirdialog as MDD
# Our normal wxApp-derived class, as usual
app = wx.App(0)
dlg = MDD.MultiDirDialog(None, title="Custom MultiDirDialog", defaultPath=os.getcwd(), # defaultPath="C:/Users/users/Desktop/",
agwStyle=MDD.DD_MULTIPLE|MDD.DD_DIR_MUST_EXIST)
if dlg.ShowModal() != wx.ID_OK:
print("You Cancelled The Dialog!")
dlg.Destroy()
paths = dlg.GetPaths()
#Print directories' path and files
for path in enumerate(paths):
print(path[1])
directory= path[1].replace('OS (C:)','C:')
print(directory)
for file in os.listdir(directory):
print(file)
dlg.Destroy()
app.MainLoop()
Run Code Online (Sandbox Code Playgroud)