I have the following following String Array:
from tkinter import filedialog as fd
pathName = fd.askopenfilename(filetypes=[('Python file','*.py')])
Output
#pathName = 'C:\Users\User\Desktop\my_program.py
Is there a way to extract the name of my file? In this example I would want the output to be "my_program"
Or a better solution for me would be to load directly file path without the file’s extension. I need ti pathName to be ‘C:\Users\User\Desktop\my_program’
I tried using tkinter’s fd.askopenfilename but it always gets the file extension too.
>Solution :
Is there a way to extract the name of my file?
You might use os.path.splitext for that purpose
import os
filename = "file.txt"
name, ext = os.path.splitext(filename)
print(name) # file
print(ext) # .txt