Check file extension

I am working on a specific program and I need it to do different things if this file is a flac file or an mp3 file. Can I just use this?

 if m == * .mp3
    ....
 elif m == * .flac
    ....

I'm not sure if this will work.

EDIT: When I use this, it tells me the invalid syntax. So what should I do?

+132
python file-extension
May 05 '11 at 14:34
source share
9 answers

Assuming m is a string, you can use endswith :

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

Case insensitivity and exclusion of a potentially large else-if chain:

 m.lower().endswith(('.png', '.jpg', '.jpeg')) 

(Thanks to Wilham Murdoch for the list of endswith arguments)

+294
May 05 '11 at 2:37
source share

os.path provides many functions for managing paths / file names. ( docs )

os.path.splitext takes a path and separates the file extension from its end.

 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." 

It gives:

 /folder/soundfile.mp3 is an mp3!
 folder1 / folder / soundfile.flac is a flac file!
+51
May 05 '11 at 15:46
source share

Check out the fnmatch module. This will do what you are trying to do.

 import fnmatch import os for file in os.listdir('.'): if fnmatch.fnmatch(file, '*.txt'): print file 
+15
May 05 '11 at 15:03
source share

or perhaps:

 from glob import glob ... for files in glob('path/*.mp3'): do something for files in glob('path/*.flac'): do something else 
+6
May 5 '11 at
source share

one simple 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 ​​(file name without extension + extension only). The second index ([1]) for this will give you just an extension. The best part is that in this way you can also easily access the file name if necessary!

+5
Mar 07 '17 at 9:11
source share
 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 
+2
May 17 '16 at 10:42
source share
 if (file.split(".")[1] == "mp3"): print "its mp3" elif (file.split(".")[1] == "flac"): print "its flac" else: print "not compat" 
+2
Jul 19 '17 at 7:27
source share

Old thread, but may help future readers ...

I would avoid using .lower () in file names, if only to make your code more platform independent. (linux is with touch, .lower () in the file name will surely ruin your logic in the end ... or, even worse, an important file!)

Why not use re ? (Although, to be even more reliable, you should check the header of the magic file for each file ... How to check the file type 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 
+2
Dec 15 '17 at 6:08
source share
 #!/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' 
+1
Sep 18 '14 at 20:53 on
source share



All Articles