How to Get File Extension in Python | DigitalOcean (2024)

Tutorial

Published on August 3, 2022

How to Get File Extension in Python | DigitalOcean (1)

By Pankaj

How to Get File Extension in Python | DigitalOcean (2)

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

We can use Python os module splitext() function to get the file extension. This function splits the file path into a tuple having two values - root and extension.

Getting File Extension in Python

Here is a simple program to get the file extension in Python.

import os# unpacking the tuplefile_name, file_extension = os.path.splitext("/Users/pankaj/abc.txt")print(file_name)print(file_extension)print(os.path.splitext("/Users/pankaj/.bashrc"))print(os.path.splitext("/Users/pankaj/a.b/image.png"))

Output:

How to Get File Extension in Python | DigitalOcean (3)
  • In the first example, we are directly unpacking the tuple values to the two variables.
  • Note that the .bashrc file has no extension. The dot is added to the file name to make it a hidden file.
  • In the third example, there is a dot in the directory name.

Get File Extension using Pathlib Module

We can also use pathlib module to get the file extension. This module was introduced in Python 3.4 release.

>>> import pathlib>>> pathlib.Path("/Users/pankaj/abc.txt").suffix'.txt'>>> pathlib.Path("/Users/pankaj/.bashrc").suffix''>>> pathlib.Path("/Users/pankaj/.bashrc")PosixPath('/Users/pankaj/.bashrc')>>> pathlib.Path("/Users/pankaj/a.b/abc.jpg").suffix'.jpg'>>> 

Conclusion

It’s always better to use the standard methods to get the file extension. If you are already using the os module, then use the splitext() method. For the object-oriented approach, use the pathlib module.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us

About the authors

How to Get File Extension in Python | DigitalOcean (4)

Pankaj

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk

How to Get File Extension in Python | DigitalOcean (6)

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

How to Get File Extension in Python | DigitalOcean (7)

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

How to Get File Extension in Python | DigitalOcean (8)

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more

How to Get File Extension in Python | DigitalOcean (9)

© 2024 DigitalOcean, LLC.Sitemap.

How to Get File Extension in Python  | DigitalOcean (2024)

FAQs

How to Get File Extension in Python | DigitalOcean? ›

We can use Python os module splitext() function to get the file extension. This function splits the file path into a tuple having two values - root and extension.

How to find the extension of a file? ›

In the search box on the taskbar, type file explorer, and in the search results, select File Explorer. In File Explorer under View, in the Show/hide group, select the File name extensions check box.

How do you check if a file extension exists in Python? ›

Using os.

isfile() method in Python is used to check if a file exists or not. It checks whether the specified path is an existing regular file or not.

What is the extension of the Python file? ›

Ans. The correct extension for Python source code files is “. py”.

How to set file extension in Python? ›

Change File Extension Using pathlib Module

Path class to represent the file path. The with_suffix method is then used to change the file extension, and rename is used for the actual file renaming.

How do I find all files with a specific extension? ›

Into search bar in Windows explorer simply type an asterisk followed by dot and extension and press enter. for example to find executable files, type *.exe and press enter. Use Windows built-in search.

How do I find the extension of an unknown file? ›

Option 1: Use an Online Tool
  1. Click Browse to open the File Upload dialog.
  2. Search for your unknown file and click Open to upload the file.
  3. Click Check File Type and wait as the file is uploaded and checked.
  4. If the web site can determine the file type, it will tell you and suggest file extensions.
Mar 19, 2024

How to identify file type in Python? ›

In Python, the python-magic module can be used to determine the file type. The installation of this module on Ubuntu Linux VM was covered in Chapter 1, Introduction to Malware Analysis. On Windows, to install the python-magic module, you can follow the procedure mentioned at https://github.com/ahupp/python-magic.

How to check for file without extension in Python? ›

Getting the name of the file without the extension:
  1. import os.
  2. print(os. path. splitext("/path/to/some/file. txt")[0])
Oct 30, 2021

How to get file name in Python? ›

Python Program to Get the File Name From the File Path
  1. import os # file name with extension file_name = os.path.basename('/root/file.ext') # file name without extension print(os.path.splitext(file_name)[0]) ...
  2. import os print(os.path.splitext(file_name)) ...
  3. from pathlib import Path print(Path('/root/file.ext').stem)

How to get filename from path in Python? ›

you can use the built-in Python function split() to split the file path into a list of individual components, and then use the rsplit() method to split the last component (which should be the file name and extension) into a list containing the file name and extension.

Is .py a Python file extension? ›

The Files with the . py extension contain the Python source code. The Python language has become very famous language now a days. It can be used for system scripting, web and software development and mathematics.

What is the best extension for Python? ›

Top 10 VS Code Extensions For Python
  1. Python. This is the essential VS Code extension for Python, developed by Microsoft itself. ...
  2. Kite AutoComplete AI Code. ...
  3. Python Preview. ...
  4. Python Snippets. ...
  5. AREPL for python. ...
  6. Better Comments. ...
  7. Python Docstring Generator. ...
  8. Python Indent.
Feb 22, 2024

How to get the filename and extension from the path in Python? ›

Get the filename, directory, extension from a path string in...
  1. Difference in path separator by OS.
  2. Get the filename (basename) from a path: os.path.basename() ...
  3. Get the directory (folder) name from a path: os.path.dirname()
  4. Get the file and directory name pair: os.path.split() ...
  5. Get the extension: os.path.splitext()
May 8, 2023

How to set file extension? ›

How to change file types on Windows 10 by changing the extension
  1. Open Windows File Explorer and select a file to change. ...
  2. Replace the old file extension with a new one to change your file. ...
  3. Push the "Enter" button on your keyboard to save your changes.
Feb 3, 2023

How do I search for a file extension in Windows? ›

To search for all files with a particular file extension, such as . txt, you can enter it like this: *. txt.

What is extension of any file? ›

A file extension is the suffix of a computer file that an operating system uses to identify the exact file type. When executing the respective file, the file extension tells the operating system which software application to open, depending on the file type.

What is the extension of a file folder? ›

Folder extensions are . dir . If you right-click a folder and go to Properties > General, it says Type: File Folder (. dir).

What is the shortcut to show file extensions? ›

What is the shortcut to show file extensions?
  1. Use Windows logo + E to open File Explorer.
  2. Press Alt , then V , and finally HF to toggle file name extensions.
Sep 19, 2023

Top Articles
Latest Posts
Article information

Author: Pres. Carey Rath

Last Updated:

Views: 5893

Rating: 4 / 5 (61 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Pres. Carey Rath

Birthday: 1997-03-06

Address: 14955 Ledner Trail, East Rodrickfort, NE 85127-8369

Phone: +18682428114917

Job: National Technology Representative

Hobby: Sand art, Drama, Web surfing, Cycling, Brazilian jiu-jitsu, Leather crafting, Creative writing

Introduction: My name is Pres. Carey Rath, I am a faithful, funny, vast, joyous, lively, brave, glamorous person who loves writing and wants to share my knowledge and understanding with you.