windows 解析 askopenfilenames() 的结果?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4116249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Parsing the results of askopenfilenames()?
提问by Paul
I'm trying to get a list of filenames from tkinter.filedialog.askopenfilenames() in Python 3.2.
我正在尝试从 Python 3.2 中的 tkinter.filedialog.askopenfilenames() 获取文件名列表。
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}
{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.
如果我尝试打印(文件),这就是我得到的结果。看起来它的格式类似于某种列表,但它似乎只是一个字符串。是我做错了什么,还是这个函数的输出实际上是一个格式化为文件列表的字符串,我需要自己拆分。
回答by eldarerathis
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.
这实际上是 Windows 版本上的一个错误,自 Python 2.6 版本左右以来一直存在。您可以在他们的 tracker 上找到问题,并且在评论中有一个解决方法(我没有亲自尝试过这个解决方法,因为我在 Linux 上,它返回一个正确的元组)。从那以后,我不知道有任何修复,并且该问题尚未标记为已关闭/已解决。
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))
回答by Will
I don't have an exact answer for you, because I'm still stuck in Python 2.x, but in my world askopenfilenames returns a tuple, so I doubt it would have changed so much going to 3.x. Maybe try casting as a list:
我没有确切的答案,因为我仍然停留在 Python 2.x 中,但在我的世界中 askopenfilenames 返回一个元组,所以我怀疑它会在 3.x 中发生很大变化。也许尝试将其转换为列表:
filelist = list(files)
Or using a list comprehension by iterating over it:
或者通过迭代使用列表理解:
filelist = [file for file in files]
回答by LPmore
I support Eldererathis's answer as the best solution I have found for Python versions 2.X (mainly 2.5 and above) versions under Linux, Mac OS X and Windows. When a tkFileDialog
calls the askopenfilename(...,multiple=1)
methods with argument multiple=1
, I could not get it work properly under Windows (Linux and Mac OS X were fine) when a single file was selected (the file is processed as a 'str'
instead of a 'tuple'
).
我支持 Eldererathis 的答案,这是我为 Linux、Mac OS X 和 Windows 下的 Python 版本 2.X(主要是 2.5 及更高版本)找到的最佳解决方案。When a tkFileDialog
calls the askopenfilename(...,multiple=1)
methods with argument multiple=1
, I could not get it work properly under Windows (Linux and Mac OS X were fine) when a single file was selected (the file is processed as a 'str'
instead of a 'tuple'
).
I tried the files = re.findall('\{(.*?)\}', files
suggested by Paul in the comments, but it did not change anything. I also tried files = tuple(files)
and files = list(files)
, but it is not a viable workaround from what I have seen.
我files = re.findall('\{(.*?)\}', files
在评论中尝试了Paul的建议,但没有任何改变。我也试过files = tuple(files)
and files = list(files)
,但从我所见,这不是一个可行的解决方法。
So far, files = tkRoot.master.splitlist(files)
is what is working under all environments I have tested (Win32, Win64, Linux32, Linux64, Mac OS X).
到目前为止,files = tkRoot.master.splitlist(files)
是在我测试过的所有环境(Win32、Win64、Linux32、Linux64、Mac OS X)下运行的。
回答by Nonsingular
I just found this question when looking up why I was getting curly brackets instead of a proper list.
我刚刚在查找为什么我得到大括号而不是正确的列表时发现了这个问题。
Here's my work around:
这是我的工作:
file_list=[]
files = files = askopenfilenames(initialdir="C:\Users\BVCAP\Videos", title="Select files")
for file in files:
file_list.append(file)
I noticed that when I was using the askopenfilenames
in my method I never looked at the object returned. I had treated it as a tuple and it worked fine. So knowing it could be iterated in a for loop, it made sense to append each item into a new blank list.
我注意到当我askopenfilenames
在我的方法中使用 时,我从未看过返回的对象。我把它当作一个元组,它工作得很好。因此,知道它可以在 for 循环中迭代,将每个项目附加到一个新的空白列表中是有意义的。
I hope this helps anyone else who encounters this bug.
我希望这可以帮助遇到此错误的其他人。