Reputation: 29495
I would like to let user delete files from a specific directory. Thus I use:
from Tkinter import Tk
from tkFileDialog import askopenfilename
Tk().withdraw()
filename = askopenfilename()
It opens a file browser and user selects a file. But user can browse to other directories in this GUI window.
I want to prevent user to browse to other directories, so that he/she won't be able to delete files from other folders. User should only be allowed to choose files from that starting directory.
How to do this?
Upvotes: 0
Views: 586
Reputation: 460
I think you'll be stuck subclassing the standard dialogs for a UI way to do it. However, for the quick and dirty it should be possible to use askopenfilename() in a loop. Something along the following:
while True:
filename = askopenfilename()
if not filename:
raise FileDeleteAbortError()
if os.path.dirname(filename) == expected_directory:
break
tkMessageBox.showwarning() # pick another file, this one's in the wrong directory
Upvotes: 0
Reputation: 1903
I don't think this is possible with the standard file dialogs. But you could write your own. Just use a treeview widget to display all files (and relevant information) in the directory. The User can multi select the files and you can delete them after the user dismisses the dialog.
Upvotes: 1