• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Remove Extension from Filename in Python

Remove Extension from Filename in Python

May 9, 2022 Leave a Comment

To remove the extension from a filename using Python, the easiest way is with the os module path.basename() and path.splitext() functions.

import os

filename = os.path.basename("C:/Users/TheProgrammingExpert/example.png")

filename_without_ext = os.path.splitext(filename)[0]

print(filename)
print(filename_without_ext)

#Output:
example.png
example

You can also use the pathlib module and Path and then access the attribute ‘stem’ to remove the extension from a filename.

from pathlib import Path

print(Path("C:/Users/TheProgrammingExpert/example.png").stem

#Output:
example

When working with files in Python, the ability to easily get the filename without the extension and remove the file extension can be useful.

With Python, there are a few ways you can remove the file extension. The easiest way is with the os module, but you can also use the pathlib module.

Using os Module to Remove Extension from Filename Using Python

The Python os module has many great functions which help us interact with the operating system of our computer.

To remove the extension from a filename using Python, the easiest way is with the os module path.basename() and path.splitext() functions.

path.basename() gets the complete filename and path.splitext() splits the filename into the filename and extension.

Below is a simple example showing you how to get the filename without file extension in Python.

import os

filename = os.path.basename("C:/Users/TheProgrammingExpert/example.png")

filename_without_ext = os.path.splitext(filename)[0]

print(filename)
print(filename_without_ext)

#Output:
example.png
example

Using pathlib Module to Remove Extension from Filename Using Python

You can also use the pathlib module to get file size in your Python code.

With the Python pathlib module, we can perform many operations to access files and directories in our environments.

You can use the pathlib module and Path, and then access the attribute ‘stem’ to remove the extension from a filename.

Below is a simple example showing you how to get remove the extension from a file using the Python pathlib module.

from pathlib import Path

print(Path("C:/Users/TheProgrammingExpert/example.png").stem

#Output:
example

Hopefully this article has been useful for you to learn how to remove the file extension from a file using Python.

Other Articles You'll Also Like:

  • 1.  How Clear a Set and Remove All Items in Python
  • 2.  Sort by Two Keys in Python
  • 3.  Python asin – Find Arcsine and Inverse Sine of Number Using math.asin()
  • 4.  Get pandas Series First Element in Python
  • 5.  Remove All Instances of Value from List in Python
  • 6.  Using Python to Sum Even Numbers in List
  • 7.  Python Even or Odd – Check if Number is Even or Odd Using % Operator
  • 8.  Get Python Dictionary Keys as List
  • 9.  pandas variance – Compute Variance of Variables in DataFrame
  • 10.  How to Group By Columns and Find Variance in pandas

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy