As the codes show, I wanted to print the file name.
def callback(): file_name = open(askopenfilename(filetypes = (("Music File", "*.mp3") ,("Video files", "*.mpeg") )),'r') print file_name
It printed this line
<open file u'C:/Users/121794/Desktop/New folder (2)/Tonight.mp3', mode 'r' at 0x01D63C80>
How can I just get the filename with its extension? e.g “Tonight.mp3”
Advertisement
Answer
Exclude the call to open
:
filename = askopenfilename(filetypes=(("Music File", "*.mp3"), ("Video files", "*.mpeg")))
If you want only the filename (excluding directory path), use os.path.basename
:
>>> import os >>> os.path.basename('a/b/c') 'c'