Python Remove Extension from Filename

Home /

Table of Contents

Extension of File

An extension of a file is the part of the file name that follows the last dot (.) and indicates the file type or format. For example, in the file name “document.docx”, the extension is “.docx”, which indicates that the file is a Microsoft Word document in the newer XML-based format.

Extensions are used by operating systems and applications to identify the file type and determine how to handle it. For example, if you double-click on a file with the “.pdf” extension, the operating system will typically launch the default PDF viewer application to open and display the file.

Some common file extensions include:

  • .txt (plain text file)
  • .docx (Microsoft Word document)
  • .pdf (Adobe PDF document)
  • .jpg (JPEG image)
  • .png (PNG image)
  • .mp3 (MP3 audio)
  • .mp4 (MP4 video)

File extensions are typically 2-4 characters long and are sometimes referred to as “file suffixes”. It’s important to note that the extension does not always accurately reflect the file type or format, as it can be easily changed or faked. Therefore, it’s always a good idea to verify the file type using other means, such as by checking the file header or using a file analysis tool.

Remove the Extension from a Filename

In Python, you can extract the extension from a filename using the ‘os.path.splitext()‘ function. Here’s an example:

Python
import os

filename = 'example.txt'
extension = os.path.splitext(filename)[1]

print(extension)  

Output:

'.txt'

In this example, the ‘os.path.splitext()‘ function takes the filename as an argument and returns a tuple containing the filename without the extension and the extension itself. We then use indexing to retrieve the extension (the second item in the tuple).

Note that the extension will include the period (.) character, so you may want to remove it if you only need the extension without the period. Here’s an updated example that removes the period:

Python
import os

filename = 'example.txt'
extension = os.path.splitext(filename)[1][1:]

print(extension)  

Output:

'txt'

In this example, we use string slicing ([1:]) to remove the first character (the period) from the extension string.

Add Extension in Filename in python

In Python, you can add an extension to a filename by using string concatenation. Here is an example:

Python
filename = "example"
extension = ".txt"
new_filename = filename + extension

In this example, the ‘filename‘ variable stores the name of the file without an extension, and the ‘extension‘ variable stores the desired extension, including the period. The ‘new_filename‘ variable uses string concatenation to combine the ‘filename‘ and ‘extension‘ variables to create the new filename with the extension included.

If you want to rename a file in Python and add an extension, you can use the ‘os‘ module to do so. Here is an example:

Python
import os

filename = "example"
extension = ".txt"
new_filename = filename + extension

os.rename(filename, new_filename)

In this example, the ‘os.rename()‘ function is used to rename the file with the new filename that includes the extension.

Replace a Extension with Another Extension

To replace an extension with another extension in Python, you can use the ‘os.path‘ module to manipulate the filename and replace the extension. Here is an example:

Python
import os

filename = "example.txt"
new_extension = ".csv"

# Split the filename and extension
base, extension = os.path.splitext(filename)

# Replace the extension with the new extension
new_filename = base + new_extension

# Rename the file
os.rename(filename, new_filename)

In this example, the ‘os.path.splitext()function is used to split the filename and extension. The ‘base‘ variable stores the filename without the extension, and the ‘extension‘ variable stores the extension, including the period.

Then, the ‘new_extension‘ variable is set to the desired new extension, including the period.

Next, the ‘base‘ and ‘new_extension‘ variables are combined using string concatenation to create the new filename with the new extension.

Finally, the ‘os.rename()‘ function is used to rename the file with the new filename that includes the new extension.

Note that this will change the file extension only, and not the file content or format.

Share The Tutorial With Your Friends
Twiter
Facebook
LinkedIn
Email
WhatsApp
Skype
Reddit

Check Our Ebook for This Online Course

Advanced topics are covered in this ebook with many practical examples.

Other Recommended Article