解析 askopenfilenames() 的结果? [英] Parsing the results of askopenfilenames()?

查看:64
本文介绍了解析 askopenfilenames() 的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 Python 3.2 中的 tkinter.filedialog.askopenfilenames() 获取文件名列表.

I'm trying to get a list of filenames from tkinter.filedialog.askopenfilenames() in Python 3.2.

    files = askopenfilenames(initialdir="C:\\Users\\BVCAP\\Videos", title="Select files")
    self.num_files.set(len(files))  

我期望输出是一个元组(或者可能是一个列表),每个元素都包含一个文件名.据我所知,它返回一个字符串,每个元素都包含在大括号 {} 中,如下所示:

I was expecting the output to be a tuple (or maybe a list) with each element containing a filename. As far as I can tell, it's returning a string with each element contained within curly-brackets {} like so:

{C:\Users\BVCAP\File1.txt} {C:\Users\BVCAP\File2.txt}

如果我尝试打印(文件),这就是我得到的结果.看起来它的格式类似于某种列表,但它似乎只是一个字符串.是我做错了什么,还是这个函数的输出实际上是一个格式化为文件列表的字符串,需要我自己拆分.

This is what I get if I try print(files). It looks like it's formatted like a list of some sort, but it seems to just be a string. Am I doing something wrong, or is the output of this function actually a string formatted like a list of files, which I need to split up by myself.

推荐答案

这实际上是 Windows 版本上的一个错误,自 Python 2.6 版本左右以来一直存在.您可以在在他们的跟踪器上找到问题,并且在评论中有一个解决方法(我没有亲自尝试过)解决方法,因为我在 Linux 上,它返回一个正确的元组).从那以后,我不知道有任何修复,并且该问题也没有被标记为已关闭/已解决.

This is actually a bug on the Windows version that has been present since around the 2.6 release of Python. You can find the issue on their tracker, and there's a workaround in the comments (I have not personally tried this workaround because I'm on Linux, which returns a proper tuple). I'm not aware of a fix since then, and the issue has not been marked as closed/resolved.

评论中建议的解决方法基本上是这样做:

The suggested workaround in the comment is essentially to do this:

master = Tk()
files = askopenfilenames(initialdir="C:\\Users\\BVCAP\\Videos", title="Select files")
files = master.tk.splitlist(files) #Possible workaround
self.num_files.set(len(files))

这篇关于解析 askopenfilenames() 的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆