Python Forum

Full Version: Exclude hidden file, filedialog.askopenfile
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I started learning Python very recently. It seems to well thought out.

In tkinter I have setup the filedialog to open the home directory on my Linux system so I can select a file. I am getting all the hidden files (.filename) in the file chooser window.

How can I exclude the hidden file (and directories)?

W.
After a some research I found a resolve that should work for you.
The showHiddenVar is used to select if the hidden files will be displayed by default or not. If you don't want to allow the users to toggle between displaying and hiding the hidden files, then simply set showHiddenBtn to '0'.

from tkinter import *
from tkinter import filedialog

root = Tk()

try:
    try:
        root.tk.call('tk_getOpenFile', '-foobarbaz')
    except TclError:
        pass

    root.tk.call('set', '::tk::dialog::file::showHiddenBtn', '1')
    root.tk.call('set', '::tk::dialog::file::showHiddenVar', '0')
except:
    pass

def openfile(event):
    fname = filedialog.askopenfilename(initialdir='/', title='Select file', filetypes=(('CSV files', '*.csv'), ('all files', '*.*')))
    print(fname)
root.bind('<Control-o>', openfile)

root.mainloop()