1
import os

path = "."

x = []

list_subfolders_with_paths = []
for  dirs in os.walk(path):
    for dir in dirs:
        x.append(dir)
            

print(len(x))
S3DEV
  • 8,768
  • 3
  • 31
  • 42
TINSEL19
  • 13
  • 3
  • By “audio files” do you mean a specific file extension, or a collection of extensions? Or, should the program be extension-agnostic, and cleverly pick out an ‘audio file’? – S3DEV Aug 23 '22 at 07:24
  • I mean path directories , like /storage/emulated/0/DCIM, I want to know if DCIM contains mp3 files , then return true , since I have all of the paths , I'll just loop through to know the ones that contains mp3 files then return true for the directory – TINSEL19 Aug 23 '22 at 07:49

3 Answers3

1
from glob import glob
list_subfolders_with_paths = glob('path/*.mp3')

Replace mp3 with audio files extensions and * is for every mp3 file in path.

  • Let's say I have a list containing this paths [user/audio, user/music, user/pictures] how do I return True if the paths contain .mp3 files? – TINSEL19 Aug 23 '22 at 07:50
  • Use "user/*/*.mp4". It will take all sub folder in user and mp4 files inside them – Anonymous89u Aug 23 '22 at 10:32
  • I don't want to get the audio file name or paths, I just want to know if the directory contains audio files – TINSEL19 Aug 23 '22 at 23:49
0

If you have a list of paths all of which have the same root directory, you can use the recursive argument from glob.

For example:

import os
from glob import glob

files = glob(os.path.join('/path/to/dir', '**', '*.mp3'), recursive=True)

Per the documentation:

If recursive is true, the pattern “**” will match any files and zero or more directories, subdirectories and symbolic links to directories.


Directory paths only:

To retrieve a list of directory paths which contain the subject files, the following can be used:

paths = list(set(map(os.path.dirname, files)))

The map function is used to get the path for each file listed in the files list. The set function is used to sub-set the list of directories to unique values only. Finally, the list function is used to convert the set of directory paths back into a list.

S3DEV
  • 8,768
  • 3
  • 31
  • 42
  • Will files be the file format i want to check for ? Like .mp3? – TINSEL19 Aug 23 '22 at 23:51
  • Yes, of course. The file extension is specified to the `glob` function. Have a look in the first section of this answer. – S3DEV Aug 24 '22 at 06:16
  • The `files` list created by the first section of the answer is used by the directory-only filter in the second part of the answer. Slot these two together and you have a solution. :-) – S3DEV Aug 24 '22 at 08:05
  • Thanks very much, I get it now – TINSEL19 Aug 25 '22 at 12:46
  • I've clicked on it – TINSEL19 Aug 25 '22 at 14:26
  • 1
    I ran out of data to come online, so I kept working on my own way to solve the problem and that's what I came up with, I couldn't check your reply on time ,so I just replied your answer since I took your time. – TINSEL19 Aug 25 '22 at 20:55
0

'''python

import os

path = "enter your directory path "

path_dir = []

for root, dirs, files in os.walk(path):

for dir in dirs:
    root_dir= os.path.join(root, dir)
    for v in os.listdir(root_dir):
        if v.endswith(".mp3") or v.endswith(".m4a"):
            if root_dir in path_dir:
                pass
            else:
                path_dir.append(root_dir)

'''

TINSEL19
  • 13
  • 3