Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get a file's name without its extension or to load the entire path with the file without the file's extension?

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"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading