Posted on 8th May 2025|90 views
How can I get a file name without extension in python?
Posted on 8th May 2025| views
In python, it is very easy to get a filename without any extension. You just need to use sliptext() like this:
import os
path="/any/file.with spaces.dot.docx"
filename = os.path.basename(path)
(file, ext) = os.path.splitext(filename)
print("Filename without any extension =", file)
print("Extension =", ext)
Output:
Filename without any extension = file.with spaces.dot
Extension = .docx