-1

I am currently writing a program where the user is asked to select multiple files.

After selection I want to be able to get just the file name with the extension.

Below is what I have currently.

import tkinter as tk
from tkinter import filedialog

root = tk.Tk()
root.withdraw()

files = filedialog.askopenfilenames()

print(files)

I am still a beginner and trying to learn the language. Right now what is returning is the whole file directory for each file.

('C:/Users/60068533/Downloads/webscrape/DN1.xls', 'C:/Users/60068533/Downloads/webscrape/DN2.xls')

I just want to get the file name with the extension, for example DN1.xls and DN2.xls.

What code can I use to achieve this? Is there a way to also convert it to a string?

Jhangir Awan
  • 17
  • 1
  • 7
  • They are already strings; there is no conversion to do. – Karl Knechtel Dec 11 '19 at 23:07
  • please see the [`os.path`](https://docs.python.org/3/library/os.path.html#module-os.path) module. You will find everything you need there. Also, try to focus your questions: the whole `tkinter` part is irrelevant for your actual question – Tomerikoo Dec 11 '19 at 23:10
  • Does this answer your question? [Extract file name from path, no matter what the os/path format](https://stackoverflow.com/questions/8384737/extract-file-name-from-path-no-matter-what-the-os-path-format) – Tomerikoo Dec 11 '19 at 23:12

1 Answers1

0

Use pathlib to get the name of the file from the path

>>> from pathlib import Path
>>> [Path(x).name for x in files]
['DN1.xls', 'DN2.xls']
abhilb
  • 5,639
  • 2
  • 20
  • 26