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.
Answers:
You need to use the askopenfilenames
method instead.
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!')
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)
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.
You need to use the askopenfilenames
method instead.
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!')
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)