• 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
  • VBA
  • 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.  Changing Python Turtle Speed with speed() Function
  • 2.  Check if Line is Empty in Python
  • 3.  Python atan2 – Find Arctangent of the Quotient of Two Numbers
  • 4.  Python rjust Function – Right Justify String Variable
  • 5.  Decrement For Loop with range() in Python
  • 6.  Change Python Turtle Shape Fill Color with fillcolor() Function
  • 7.  Python Remove First Element from List
  • 8.  How to Concatenate Tuples in Python
  • 9.  pandas product – Get Product of Series or DataFrame Columns
  • 10.  Draw Rectangle in Python Using turtle Module

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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy