1

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.

4
  • 1
    Sorry, not sure if I am misunderstanding here, but are you saying that the user will actually manually select 500 files from the file dialogue? If there is a systematic way to determine which files you are using rather than user input then I would recommend using something like os.walk() to go through the directory structure and pick up details of the files you need. Please could you provide a little more information on your requirement. Jul 25, 2013 at 16:46
  • @ChrisProsser, yes it's manual, b/c it's more flexible to choose what you want. Thank you for your recommendation. I have changed code, could you check it?
    – Shengen
    Jul 25, 2013 at 17:05
  • @ChrisProsser I noticed the bug here, result returns to a unicode string. Somehow, sometime it returns in an expect list.
    – Shengen
    Jul 25, 2013 at 18:56
  • 1
    @Shengen: Whether it returns a unicode string or not depends on how many files you selected -- relatively smaller numbers of them appear to work, but not a huge amount, which appears to be a bug you've discovered.
    – martineau
    Jul 25, 2013 at 19:02

1 Answer 1

2

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.

4
  • 2
    Yes, the returning a unicode string is a known bug -- actually, the workaround I use is file_list = re.findall('\{(.*?)\}', file_list). Regardless, even with that fix in place, I am able to consistently reproduce the OP's bug in Python 2.7.5 when I select a large number of files, otherwise it's fine. The bug appears to be deep inside the Tkinter module or maybe the tk/tcl library itself, because I can see it happened already at several of the upper pure-Python levels.
    – martineau
    Jul 25, 2013 at 18:17
  • FWIW, here's a SO question related to the unicode string issue: Parsing the results of askopenfilenames()?. A comment to the accepted answer, BTW, says the splitlist() didn't work.
    – martineau
    Jul 25, 2013 at 18:55
  • @martineau According to link, when I set master=Tk(), and returns out an error: name 'Tk' is not defined , what should I import for it? import Tkinter?
    – Shengen
    Jul 25, 2013 at 19:27
  • 1
    @Shengen: To run the code in the link add from Tkinter import * and from tkFileDialog import askopenfilenames.
    – martineau
    Jul 25, 2013 at 19:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.