Post

PythonPython

v
veerubhotla sowmithri

Posted on 28th January 2023|35 views

0
votes

Python Filename Without Extension

How can I get a file name without extension in python?

Answers
P
rushi chowdary

Posted on 28th January 2023

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

 

Write your answer

STILL GOT QUERIES?

Get a Live FREE Demo
  • Explore the trending and niche courses and learning maps
  • Learn about tuition fee, payment plans, and scholarships
  • Get access to webinars and self-paced learning videos