Python; User Prompts; choose multiple files

Question:

I would like to have my code bring up a window where you can select multiple files within a folder and it assigns these filenames to elements of a list.

Currently, I can only select a single file at a time and it assigns the filename to a single variable.

from Tkinter import Tk
from tkFileDialog import askopenfilename

Tk().withdraw()
filename = askopenfilename()

Thank you.

Asked By: Teddy_Tort

||

Answers:

You need to use the askopenfilenames method instead.

Answered By: Rob Watts

You can encapsulate all that in a function:

def get_filename_from_user(message):
  root = Tk()
  root.withdraw()
  filename = tkFileDialog.askopenfilename(title=message)
  return filename

Then you can call it as many times as you like:

filename1 = get_filename_from_user('select the first file!')
filename2 = get_filename_from_user('select another one!')
filename3 = get_filename_from_user('select one more!')

Unless you have tons of files you want to select. Then you probably want to use askopenfilenames:

files = tkFileDialog.askopenfilenames(parent=root,title='Choose a file or LOTS!')
Answered By: erewok
from easygui import fileopenbox
files = []
#how many file you want choice
fileCount = int(input("How many file need open"))
for x in range(fileCount):
    files.append(fileopenbox())
print(files)
Answered By: HungHT1890
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.