Quantity limitation for dialog window selecting files?

时光怂恿深爱的人放手 提交于 2019-12-19 04:40:44

问题


Python version: 2.7 Tk version: 8.5

Refer to my previous question how to add the selected files from dialog window to a dictionary?

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"

if I selec all 500 files, it only gives

mydict {}

Any solution? Thanks.


回答1:


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)

Hope this helps.



来源:https://stackoverflow.com/questions/17863290/quantity-limitation-for-dialog-window-selecting-files

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!