ExtensionsTech

Step by Step Instructions to Get a File Extension in Python?

A file extension distinguishes the arrangement of a file and is set after the filename, for instance, abc.txt, prog.py, where .txt shows a text file and .py demonstrates a Python file. Knowing the file extension can once in a while become fundamental in programming projects. Diverse programming dialects give a few capacities that can assist you with performing procedures on the OS file way.

Can’t do your python assignments since you can’t discover the file extension of a file you made or utilized in the program? In this speedy instructional exercise, you will figure out how to separate the file extension utilizing Python’s underlying provisions. There are two module techniques accessible for getting the file extension from the file way.

On the off chance that you get homework to discover file extension in Python and you do not know how you can accomplish that, then, at that point, this article is a file for you.

1. Using os.path Module to get the file in Python

The os.path module comprises valuable capacities appropriate for the Operating System (OS) you’re running your Python program on. Utilizing these capacities, you can open, close, refresh and get data on OS file ways.

This module contains a splittext() work, that isolates the root and extension from the file way. Utilizing this capacity, we acquire the tuple upsides of the two factors as a string.

root – returns the parent registry and filename (foundation) of the way

extension – returns the extension of the way

We should consider a model that can assist you with seeing how you can apply this capacity to your venture. Assume a file with the accompanying way:

/Users/user/Documents/sampledoc.docx

To get the .docx extension from this way, import the module, proclaim the root and extension factors, and relegate the qualities with the os.path.splittext(file way).

Code:

import os
path = '/Users/user/Documents/sampledoc.docx'
root, extension = os.path.splitext(path) print('Root:', root)
print('extension:', extension)

Print the values and the extracted extension will be printed in the output as Root:

/Users/user/Documents/sampledoc
Extension: .docx

Presently you have the file extension isolated! You can likewise recover the file way again by putting the root and extension and printing it together.

Utilizing the above code furnishes you the extension alongside the speck. Assuming you need to eliminate the speck before the extension, utilize the accompanying code:

import os.path
my_path = r'path where the file is stored\file name.file extension'
ext = os.path.splitext(my_path)[1][1:]
print(ext)

The yield of this file will contain the text of the extension without the “.” separator. For this situation, it will be “docx“. It is favored that you utilize the splittext() technique when the OS module is imported and being utilized.

2. Using the pathlib module in Python

Python gives more than one answer for separate file extension from the file way so you can get your work done without any problem. The pathlib module contains different classes addressing framework file ways supporting distinctive working frameworks. It includes utility capacities that you can use to get the root, file name, and file extension from a file way.

pathlib.Path() – accepts the file way as a string contention and returns another Path object.

It contains the accompanying credits:

parent – returns the parent registry of the way

name – returns the filename alongside the extension of the way

suffix – returns the extension of the way

This is the manner by which you can execute the pathlib ascribes:

import pathlib
path = pathlib.Path('/Users/user/Documents/sampledoc.docx')
print('Parent:', path.parent)
print('Filename:', path.name)print('Extension:', path.suffix)

Output:

Parent: /Users/user/Documents
Filename: sampledoc.docx
Extension: .docx

There may be documents with numerous augmentations, for example, .tar.tz. The addition trait gives just the singleton extension, and you may lose both the postfixes of the file way. On the off chance that your schoolwork task requests you to track down every one of the expansions from the file way, then, at that point, you need to utilize an alternate strategy. Fortunately, the pathlib.Path gives the postfixes trait, which records all the additions of the given file.

Utilize the code underneath:

import pathlib
path = pathlib.Path('/Users/user/Documents/app_sample.tar.gz')
print('Parent:', path.parent)print('Filename:', path.name)print('Extension:',''.join(path.suffixes))

Output:

import pathlib
path = pathlib.Path('/Users/user/Documents/app_sample.tar.gz')
print('Parent:',path.parent)print('Filename:', path.name)
print('Extension:',''.join(path.suffixes))

You can see that utilizing the quality of the addition, both of the augmentations were printed. You can likewise store the extension esteem in a different string variable so you can undoubtedly utilize it again without the need of calling the traits over and over. The pathlib module is liked to utilize when you have an item arranged way to deal with your program. Additionally, it proves to be useful when you are discovering a way that could get you both single and various postfixes from the file way.

End

Programming can be fun in the event that you realize how to execute the capacities accurately! There are many programming arrangements accessible on the web, yet they can once in a while be befuddling. We are here to make learning simple for you.

We have joined the absolute most effortless manners by which you can recover the postfix of any file way on your OS. The splittext() capacity of the os.path module is the standard technique; in any case, assuming you need to make an article arranged program, then, at that point utilizing pathlib module is awesome. We trust this assists you with doing your job!

Learn more from development like we have resolved WordPress internal server error 500.

Back to top button