30

what I want to do is to select multiple files using the tkinter filedialog and then add those items to a list. After that I want to use the list to process each file one by one.

#replace.py
import string
def main():
        #import tkFileDialog
        #import re
        #ff = tkFileDialog.askopenfilenames()
        #filez = re.findall('{(.*?)}', ff)
        import Tkinter,tkFileDialog
        root = Tkinter.Tk()
        filez = tkFileDialog.askopenfilenames(parent=root,title='Choose a file')

Now, I am able to select multiple files, but I dont know how to add those filenames to the list. any ideas?

faraz
  • 2,603
  • 12
  • 39
  • 61
  • 1
    Note if you can't select multiple files make sure you are using `askopenfilenames` with an `s` at the end. I was using `askopenfilename` and wondering why it was only letting me select 1 file. – User Sep 21 '22 at 23:25

4 Answers4

39

askopenfilenames returns a string instead of a list, that problem is still open in the issue tracker, and the best solution so far is to use splitlist:

import Tkinter,tkFileDialog

root = Tkinter.Tk()
filez = tkFileDialog.askopenfilenames(parent=root, title='Choose a file')
print root.tk.splitlist(filez)

Python 3 update:

tkFileDialog has been renamed, and now askopenfilenames directly returns a tuple:

import tkinter as tk
import tkinter.filedialog as fd

root = tk.Tk()
filez = fd.askopenfilenames(parent=root, title='Choose a file')
A. Rodas
  • 20,171
  • 8
  • 62
  • 72
  • In line 4 of your code here, what does 'root' mean? Is that the console window that the user started the script from? –  Aug 12 '15 at 21:27
  • @danielcg No, it is a name for the top-level (root) window of the Tkinter app. It is opened alongside the console where the Python program is executed. – A. Rodas Aug 12 '15 at 21:43
  • Sorry for the stupid question haha but how would I make the console window the parent of the askopenfilenames dialog? –  Aug 12 '15 at 21:48
  • 4
    I believe that this is now out of date? – Mad Physicist Aug 02 '18 at 19:16
18
askopenfilenames

returns a tuple of strings, not a string. Simply store the the output of askopenfilenames into filez (as you've done) and pass it to the python's list method to get a list.

filez = tkFileDialog.askopenfilenames(parent=root,title='Choose a file')
lst = list(filez)

>>> type(lst)
<type 'list'>
Belial
  • 821
  • 1
  • 9
  • 12
5

Putting together parts from above solution along with few lines to error proof the code for tkinter file selection dialog box (as I also described here).

import tkinter as tk
from tkinter import filedialog
root = tk.Tk()
root.withdraw()
root.call('wm', 'attributes', '.', '-topmost', True)
files = filedialog.askopenfilename(multiple=True) 
%gui tk
var = root.tk.splitlist(files)
filePaths = []
for f in var:
    filePaths.append(f)
filePaths

Returns a list of the paths of the files. Can be stripped to show only the actual file name for further use by using the following code:

fileNames = []
for path in filePaths:
    name = path[46:].strip() 
    name2 = name[:-5].strip() 
    fileNames.append(name2)
fileNames

where the integers (46) and (-5) can be altered depending on the file path.

mvx
  • 320
  • 4
  • 15
0

In Python 3, the way it worked for me was this (respect lowercase):

from tkinter.filedialog import askopenfilenames

filenames = askopenfilenames(title = "Open 'xls' or 'xlsx' file") 

for filename in filenames:
    # print or do whatever you want

I hope you find it useful! Regards!

juanpb12
  • 125
  • 1
  • 9
  • Is there a method to alter this code in order to have the filenames stored in the order by which they are selected? Right now, the code only goes in alphanumeric order from what my system is showing. – stanatomist Mar 01 '22 at 19:38