对话框选择文件的数量限制? [英] Quantity limitation for dialog window selecting files?

查看:105
本文介绍了对话框选择文件的数量限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python版本:2.7 Tk版本:8.5

Python version: 2.7 Tk version: 8.5

请参阅我先前的问题如何将对话框窗口中的选定文件添加到字典中?

我正在尝试从对话框窗口中选择500个文件,并将其名称提取为字典的键.文件总大小约为200M. 我不知道为什么我没有空字典.但是,如果我每次选择的文件较少(例如100个),则每次的效果都很好.所以我的问题是,对话框窗口选择文件或字典中的键是否有数量限制?

I am trying to select 500 files from dialog window and extract their name as keys for a dictionary. Total files size is around 200M. I have no idea why I got an empty dictionary. However, if I choose less files like 100 each time, it works very well at each time. So my question is that is there any quantity limitation for dialog window selecting files or for keys in a dictionary?

sys.path.append("C:\MY PATH")
os.environ['PATH']+=";C:\MY PATH"

print "Please select your txt files in the dialog window >>"
filez = tkFileDialog.askopenfilenames(parent=root,multiple='multiple',title='Choose a file',filetypes=[('txt file','.txt'),('All files','.*')])

mydict = {}
for FilenameWithPath in filez:
    path, Filename = os.path.split(str(FilenameWithPath))
##    Filename = sys.path.basename(FilenameWithPath)
    mydict[Filename] = len(mydict)
print "mydict " + str(mydict)   
print "\n"

如果我选择全部500个文件,则只会给出

if I selec all 500 files, it only gives

mydict {}

有解决方案吗?谢谢.

推荐答案

我想我可以看到问题出在哪里.我做了一些调试,发现返回到filez的数据类型是一个unicode字符串(您似乎希望在其中找到列表或元组).

I think I can see where the issue is. I have done a little debugging and found that the data type returned into filez is a unicode string (where you seem to be expecting a list or tuple).

您需要在循环之前进行转换.如果您的文件名均不包含空格,则应该很简单:

You will need to convert this before your loop. If none of your file names contain spaces this should just be a simple matter of:

file_list = files.split()

但是,如果不是这种情况,则以上命令将不起作用,并且文件名中包含用大括号{}括起来的空格的文件名.

However, if this is not the case then the above will not work and and filenames that contain spaces with be enclosed with curly braces {}.

根据此页面,这实际上可能是一个错误.但是,也建议您解决此问题,以将字符串转换为元组:

This may actually be a bug according to this page. However, a work around is also suggested to convert the string to a tuple:

file_list=  master.tk.splitlist(filez)

希望这会有所帮助.

这篇关于对话框选择文件的数量限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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