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.
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:
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:
Hope this helps.