We can check whether a file or path exist using os.path.exists(path_name) and whether it is a file or directory using os.path.isfile() and os.path.isdir() respectively after importing os module in python. But how can we check if the directory specified contains particular types of files (music, image, video, doc, xls etc.)
Asked
Active
Viewed 814 times
2
-
Does this answer your question? https://stackoverflow.com/questions/5899497/how-can-i-check-the-extension-of-a-file – go2nirvana Nov 12 '20 at 09:32
-
1Maybe use the Python analogue of the Linux `file --mime-type` command... https://pypi.org/project/python-magic I get `audio/XXX` for MP3 and AAC files for example. – Mark Setchell Nov 12 '20 at 09:55
2 Answers
1
One way is to use os.listdir(path)
to get a list of all entries in that directory, then loop through that list to identify the filetypes.
Here's an example using most audio file extensions:
import os
path = "./" #path to your directory
dir = os.listdir(path)
audio_extenions =["3gp","aa","aac","aax","act","aiff","alac","amr","ape","au","awb","dct","dss","dvf","flac","gsm","iklax","ivs","m4a","m4b","m4p","mmf","mp3","mpc","msv","nmf","ogg","oga","mogg","opus","ra","rm","raw","rf64","sln","tta","voc","vox","wav","wma","wv","webm","8svx","cda"]
for file in dir:
if os.path.isfile(file):
if file.endswith(tuple(audio_extenions)):
#directory contains audio files
Further code could be implemented to determine if all files in a directory are the same type or not.

Herbs
- 180
- 1
- 6
-
This only answers if the file ends with .mp3, and does not include wav, ogg - or any future formats that might be invented and break your code, and it makes sense for a library to handle with them as well. – amit Nov 12 '20 at 09:37
-
1The question was "how can we check if the directory specified contains particular types of files" and this answer clearly shows the OP how to identify particular types of files. I expect anyone to read this answer and have a good idea of how to implement other file types they might need to identify. – Herbs Nov 12 '20 at 09:42
-
No, because you can add as much file types as you want - it will still not be forward compatible. – amit Nov 12 '20 at 09:43
-
1Creating a program that can identify file types such as audio, image or video with extensions that have not been invented yet does not seem feasible. Also, the OP has not requested a forward compatible solution. – Herbs Nov 12 '20 at 09:47
1
You can try filtering os.listdir()
, such as if you want to check if the C:\Test folder has .mp3 or .mp4 files:
import os
any(True for x in os.listdir(r'C:\Test') if x.split(".")[-1] in ['mp3','mp4','ogg','wav'])

Wasif
- 14,755
- 3
- 14
- 34
-
This only answers if the file ends with .mp3 and mp4, and does not include wav, ogg - or **any future formats that might be invented and break your code**, and it makes sense for a library to handle with them as well. – amit Nov 12 '20 at 09:39
-
-
And it will break once a new format is invented. IIUC, The OP is asking for a canonical way to handle it, and will not break, same as os.path.isfile() doesn't break if you import the code in a different OS, or if your kernel changes how it handle file directories. (Forward compatability) – amit Nov 12 '20 at 09:42
-
1@amit Stop spamming reasonable answers w. bizarre reqs. There is no way to "smell" music files somehow. You need to know the extensions. And you will need an app able to read them. Otherwise, it's just a file. To the os isfile works because it's just a file, you are not asking it more. If the files did change (new file system), guess what isfile wouldn't work until the os was adjusted. You're not even the OP really don't understand what you are getting at. There are file samplers that dont use extensions and operate on file headers but even they need adjust for new datatypes. – JL Peyret Nov 13 '20 at 19:09
-
1And speaking of OS and music, you know how things like Windows tend to "know" what a file is and display subtle cues in Explorer? Well, drop in some oggs on a system that doesn't know about them, and they are *just files*. – JL Peyret Nov 13 '20 at 19:14