1

I am trying to get just the file name of the selected file in tkinter file dialog

Here's my code:

def browseFile(self):
    root = tk.Tk()
    root.withdraw()
    file_path = askopenfilename(filetypes=(("Video files", "*.mp4;*.flv;*.avi;*.mkv"),
                                       ("All files", "*.*") ))

    print file_path

What I am getting with this code is the whole path of the selected file, where I only need the file name. How can I do it?

results with my code:

 C:/Users/Guest/vid1.mp4

what I want:

 vid1.mp4
KPA
  • 173
  • 2
  • 4
  • 12
  • see [os.path.split](https://docs.python.org/3.4/library/os.path.html#os.path.split) – msw Feb 18 '16 at 14:11

1 Answers1

7
>>> import os
>>> s = "C:/Users/Guest/vid1.mp4"
>>> os.path.split(s)
('C:/Users/Guest', 'vid1.mp4')
>>> os.path.split(s)[1]
'vid1.mp4'

Alternatively,

>>> os.path.basename(s)
'vid1.mp4'
Kevin
  • 74,910
  • 12
  • 133
  • 166
  • yup same results but is there any way like file_path.name() other than splitting strings? – KPA Feb 18 '16 at 14:12
  • No, there's no method belonging to the `file_path` object that will find the name for you. It's an ordinary string object, and doesn't even know it contains a filename. – Kevin Feb 18 '16 at 14:13