Tkinter askopenfilename not returning as expected when user clicks "Cancel."

Question:

According to the documentation, filedialog.askopenfilename() is supposed to return an empty string if the user clicks “Cancel,” but it’s not doing that, and I can’t figure out what it’s actually returning.

I made this little test program, and the behavior is identical to my actual project:

from tkinter import *
from tkinter import filedialog

name = filedialog.askopenfilename()
if name == '':
    print("Nothing chosen")
else:
    print(name)

What ends up printing when the user clicks cancel is a pair of empty parentheses ()

Replacing ” with ‘()’ does not change anything.

Any help figuring out what is happening is appreciated. Thank you.

UPDATE: Got it working thanks to @PaulRooney’s suggestion, but now, the first time I run this section of code (in my full project), it returns the empty tuple. For every subsequent run, it returns an empty string.

Simply checking for both works for what I need, but it’s bizarre behavior.

I’m using Python 3 on Linux Mint.

Asked By: Brandon Fedie

||

Answers:

Hmm… Make sure you are using using Python 3 because this won’t work on Python 2
It worked for me, when I changed the brackets to “” (i’m on Python 2)

Answered By: Anonymous

if name will work just fine.

from tkinter import *
from tkinter import filedialog

name = filedialog.askopenfilename()
if name:
    print(name)
else:
    print("Nothing chosen")
Answered By: user4171906

Didn’t see this post till after I answered an older but similar question!

Basically, clicking Cancel will return an empty string…
Unless you actually select/highlight a file first and then click cancel.
This seems to return an empty tuple!!!

(I didn’t try with initialdir and initialfile options but am guessing they also cause an empty tuple to be returned… at least initially?)

Using python 2.6.6 (IDK, ask RedHat)
Running the following code produces the subsequent results

f_picked = tkFileDialog.askopenfilename()
test = type(f_picked)
print (test)

Results:
<type 'unicode'> # Nothing selected, Cancel clicked
<type 'tuple'> # File selected, Cancel clicked
<type 'str'> # File selected, OK clicked
<type 'tuple'> # Multiple files selected, OK clicked

Answered By: canyonblue77

I have a File -> Open menu item that displays an askopenfilename dialogue. I am finding that with python 2.7 and 3.6, the first time I use the dialogue and select Cancel or close the dialogue from the window border, it returns an empty tuple instead of ''. Every time thereafter the dialogue returns '' on Cancel or window close.

Perhaps this is some sort of feature but it feels like a bug, particularly for this dialogue that doesn’t support selection of multiple files. An empty tuple would make sense from askopenfilenames.

I’ve settled on this simple test and cleanup for the file name returned by the askopenfilename dialogue:

if str(filename) == '()':
    filename = ''
Answered By: MFB

I’m using python 3.8 and from my experience
filedialog when is closed does not always return an empty string, to handle it you can use the type keyword to identify the type of data from filedialog and then use the if statement to handle it

    from tkinter import filedialog

    filePath = filedialog.askopenfilename()

    if str(type(filePath))=="<class 'tuple'>" or filePath == '':
        # This part will be executed when the user cancels or exit
        print('filePath is not contain path')

    else:
        print('your path',filePath)
Answered By: Alex Katarani
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.