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.
You can use pathlib.Path()
to work with path
import pathlib
p = pathlib.Path(r'C:\Users\User\Desktop\my_program.py')
print( p.stem ) # 'C:\Users\User\Desktop\my_program'
print( p.parent ) # 'C:\Users\User\Desktop'
print( p.suffix ) # '.py'
you can also use /
to create new path
print( p.parent / 'subfolder' / 'new.py' )
# 'C:\Users\User\Desktop\subfolder\new.py'
Doc: pathlib