How can I check the extension of a file?
Asked Answered
D

14

303

I'm working on a certain program where I need to do different things depending on the extension of the file. Could I just use this?

if m == *.mp3
   ...
elif m == *.flac
   ...
Disparage answered 5/5, 2011 at 14:34 Comment(2)
use python re module (regex) for matchingSade
Does this answer your question? Extracting extension from filename in PythonSantosantonica
S
621

Assuming m is a string, you can use endswith:

if m.endswith('.mp3'):
...
elif m.endswith('.flac'):
...

To be case-insensitive, and to eliminate a potentially large else-if chain:

m.lower().endswith(('.png', '.jpg', '.jpeg'))
Summer answered 5/5, 2011 at 14:37 Comment(6)
ext = m.rpartition('.')[-1]; if ext == will be much more efficientConsume
@Consume why not use .split('.')[-1]? Or is rpartition really high efficiency?Croze
@Stevoisiak, I think you misplaced your comment as this solution works even in the case you point outTessi
This doesn't account for folder names with periods. C:/folder.jpg is a valid path. You can confirm if it is a file or folder with os.path.isfile(m)Fast
@Tessi Thanks for pointing that out, I fixed my commentFast
This isn't file type cheking, if some one change file format so that can makes bugs in program.Morganne
B
80

os.path provides many functions for manipulating paths/filenames. (docs)

os.path.splitext takes a path and splits the file extension from the end of it.

import os

filepaths = ["/folder/soundfile.mp3", "folder1/folder/soundfile.flac"]

for fp in filepaths:
    # Split the extension from the path and normalise it to lowercase.
    ext = os.path.splitext(fp)[-1].lower()

    # Now we can simply use == to check for equality, no need for wildcards.
    if ext == ".mp3":
        print fp, "is an mp3!"
    elif ext == ".flac":
        print fp, "is a flac file!"
    else:
        print fp, "is an unknown file format."

Gives:

/folder/soundfile.mp3 is an mp3!
folder1/folder/soundfile.flac is a flac file!
Borecole answered 5/5, 2011 at 15:46 Comment(2)
This method ignores leading periods so /.mp3 is not considered an mp3 file. This is however the way a leading space should be treated. E.g .gitignore is not a file formatDetrital
This doesn't account for folder names with periods. (C:/folder.jpg/file.mp3 is a valid path). You can exclude those with os.path.isfile(m)Fast
M
78

Use pathlib From Python3.4 onwards.

from pathlib import Path
Path('my_file.mp3').suffix == '.mp3'

If you are working with folders that contain periods, you can perform an extra check using

Path('your_folder.mp3').is_file() and Path('your_folder.mp3').suffix == '.mp3'

to ensure that a folder with a .mp3 suffix is not interpreted to be an mp3 file.

Murillo answered 11/6, 2018 at 5:31 Comment(0)
P
21

Look at module fnmatch. That will do what you're trying to do.

import fnmatch
import os

for file in os.listdir('.'):
    if fnmatch.fnmatch(file, '*.txt'):
        print file
Punctuation answered 5/5, 2011 at 15:3 Comment(0)
S
9

or perhaps:

from glob import glob
...
for files in glob('path/*.mp3'): 
  do something
for files in glob('path/*.flac'): 
  do something else
Stickybeak answered 5/5, 2011 at 14:58 Comment(0)
W
9

one easy way could be:

import os

if os.path.splitext(file)[1] == ".mp3":
    # do something

os.path.splitext(file) will return a tuple with two values (the filename without extension + just the extension). The second index ([1]) will therefor give you just the extension. The cool thing is, that this way you can also access the filename pretty easily, if needed!

Wunderlich answered 7/3, 2017 at 9:11 Comment(0)
B
6

An old thread, but may help future readers...

I would avoid using .lower() on filenames if for no other reason than to make your code more platform independent. (linux is case sensistive, .lower() on a filename will surely corrupt your logic eventually ...or worse, an important file!)

Why not use re? (Although to be even more robust, you should check the magic file header of each file... How to check type of files without extensions in python? )

import re

def checkext(fname):   
    if re.search('\.mp3$',fname,flags=re.IGNORECASE):
        return('mp3')
    if re.search('\.flac$',fname,flags=re.IGNORECASE):
        return('flac')
    return('skip')

flist = ['myfile.mp3', 'myfile.MP3','myfile.mP3','myfile.mp4','myfile.flack','myfile.FLAC',
     'myfile.Mov','myfile.fLaC']

for f in flist:
    print "{} ==> {}".format(f,checkext(f)) 

Output:

myfile.mp3 ==> mp3
myfile.MP3 ==> mp3
myfile.mP3 ==> mp3
myfile.mp4 ==> skip
myfile.flack ==> skip
myfile.FLAC ==> flac
myfile.Mov ==> skip
myfile.fLaC ==> flac
Bebop answered 15/12, 2017 at 6:8 Comment(0)
F
6

You should make sure the "file" isn't actually a folder before checking the extension. Some of the answers above don't account for folder names with periods. (folder.mp3 is a valid folder name).


Checking the extension of a file:

import os

file_path = "C:/folder/file.mp3"
if os.path.isfile(file_path):
    file_extension = os.path.splitext(file_path)[1]
    if file_extension.lower() == ".mp3":
        print("It's an mp3")
    if file_extension.lower() == ".flac":
        print("It's a flac")

Output:

It's an mp3

Checking the extension of all files in a folder:

import os

directory = "C:/folder"
for file in os.listdir(directory):
    file_path = os.path.join(directory, file)
    if os.path.isfile(file_path):
        file_extension = os.path.splitext(file_path)[1]
        print(file, "ends in", file_extension)

Output:

abc.txt ends in .txt
file.mp3 ends in .mp3
song.flac ends in .flac

Comparing file extension against multiple types:

import os

file_path = "C:/folder/file.mp3"
if os.path.isfile(file_path):
    file_extension = os.path.splitext(file_path)[1]
    if file_extension.lower() in {'.mp3', '.flac', '.ogg'}:
        print("It's a music file")
    elif file_extension.lower() in {'.jpg', '.jpeg', '.png'}:
        print("It's an image file")

Output:

It's a music file
Fast answered 12/11, 2020 at 20:13 Comment(0)
S
4
import os
source = ['test_sound.flac','ts.mp3']

for files in source:
   fileName,fileExtension = os.path.splitext(files)
   print fileExtension   # Print File Extensions
   print fileName   # It print file name
Stolen answered 17/5, 2016 at 10:42 Comment(0)
T
2
#!/usr/bin/python

import shutil, os

source = ['test_sound.flac','ts.mp3']

for files in source:
  fileName,fileExtension = os.path.splitext(files)

  if fileExtension==".flac" :
    print 'This file is flac file %s' %files
  elif  fileExtension==".mp3":
    print 'This file is mp3 file %s' %files
  else:
    print 'Format is not valid'
Tompion answered 18/9, 2014 at 20:53 Comment(0)
J
2
if (file.split(".")[1] == "mp3"):
    print "its mp3"
elif (file.split(".")[1] == "flac"):
    print "its flac"
else:
    print "not compat"
Jehovist answered 19/7, 2017 at 7:27 Comment(2)
This won't work for files containing multiple .s, for example some.test.file.mp3Tapis
You could do [-1] to capture that edge case.Jehovist
S
1

If your file is uploaded then

import os


file= request.FILES['your_file_name']          #Your input file_name for your_file_name
ext = os.path.splitext(file.name)[-1].lower()


if ext=='.mp3':
    #do something

elif ext=='.xls' or '.xlsx' or '.csv':
    #do something

else:
    #The uploaded file is not the required format
Suber answered 20/11, 2021 at 1:13 Comment(0)
B
0
file='test.xlsx'
if file.endswith('.csv'):
    print('file is CSV')
elif file.endswith('.xlsx'):
    print('file is excel')
else:
    print('none of them')
Baskerville answered 29/5, 2019 at 11:57 Comment(1)
Describe something about your answer will help others to understand easily , refer stackoverflow.com/help/how-to-answerMelquist
H
0

I'm surprised none of the answers proposed the use of the pathlib library.

Of course, its use is situational but when it comes to file handling or stats pathlib is gold.

Here's a snippet:


import pathlib


def get_parts(p: str or pathlib.Path) -> None:
    p_ = pathlib.Path(p).expanduser().resolve()
    print(p_)
    print(f"file name: {p_.name}")
    print(f"file extension: {p_.suffix}")
    print(f"file extensions: {p_.suffixes}\n")


if __name__ == '__main__':
    file_path = 'conf/conf.yml'
    arch_file_path = 'export/lib.tar.gz'

    get_parts(p=file_path)
    get_parts(p=arch_file_path)

and the output:

/Users/hamster/temp/src/pro1/conf/conf.yml
file name: conf.yml
file extension: .yml
file extensions: ['.yml']

/Users/hamster/temp/src/pro1/conf/lib.tar.gz
file name: lib.tar.gz
file extension: .gz
file extensions: ['.tar', '.gz']

Hautegaronne answered 23/11, 2022 at 9:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.