Python如何通过单击lis从文件列表中打开文件

2024-04-19 02:58:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我希望通过单击列表从文件列表中打开一个文件。该文件是重播游戏的变量列表。Glob.glob获取要选择1的.txt文件列表。你知道吗

我可以得到一个目录中的文件列表,我需要选择一个文件,其中包含重新运行游戏的数据


Tags: 文件数据目录txt游戏列表glob
1条回答
网友
1楼 · 发布于 2024-04-19 02:58:40

假设您的意思是对于桌面应用程序,您可能需要askopenfilename。这里有一个简单的包装。你知道吗

try:
    # Python 2
    from Tkinter import Tk
    from tkFileDialog import askopenfilename
except ImportError:
    # Python 3
    from tkinter import Tk
    from tkinter.filedialog import askopenfilename


def get_file_dialog(title, initial_dir, filetypes):
    """
    title is the text to show in the title bar of the dialog
    initial_dir is the directory to show when opening the dialog
    filetypes is a list/tuple like this:
        (('All Files', '.*'), ('MP3', '.mp3'), ('WAV', '.wav'))
    """
    tk_temp = Tk()
    tk_temp.withdraw()  # Returns a value we don't need
    filepath = askopenfilename(filetypes=filetypes,
                               initialdir=initial_dir,
                               title=title)
    if filepath is ():
        filepath = ''
    return filepath

相关问题 更多 >