1

I have a directory structure like this

Root\
    DirA\
        DirX\
            File.aaa
            File.bbb
        File.ccc
        File.ddd
    DirB\
        File.aaa
        File.ccc
        File.ddd
        File.eee
    File.fff

using python I want to get a list of all file types like this:

['aaa','bbb','ccc','ddd','eee','fff']
Mahmoud Samy
  • 2,822
  • 7
  • 34
  • 78
  • 2
    Google, "python scan directory for file extensions" and work with all the samples that pop up that are really close to what you want. If you can get all the file names, it's a matter of looking for the last dot and doing some string manipulation. – RacerNerd Feb 28 '14 at 01:36
  • 1
    The following example does almost what you want: https://stackoverflow.com/questions/4582550/file-walking-in-python – Javier Feb 28 '14 at 01:37
  • 1
    See [`os.walk`](http://docs.python.org/3/library/os.html#os.walk) and [`os.path.splitext`](http://docs.python.org/3/library/os.path.html#os.path.splitext) – poke Feb 28 '14 at 01:39
  • possible duplicate of [Count number of files with certain extension in Python](http://stackoverflow.com/questions/1320731/count-number-of-files-with-certain-extension-in-python) – dbn Feb 28 '14 at 02:02
  • 1
    @dbw http://stackoverflow.com/questions/1320731/count-number-of-files-with-certain-extension-in-python counting only one type of files with a predefined extension. It doesn't provide a generic solution. – Mahmoud Samy Feb 28 '14 at 02:44

3 Answers3

1
import os

def get_file_types(directory):
    file_ext = []
    for root, directories, files in os.walk(directory):
        for filename in files:
            filepath = os.path.join(root, filename)
            fileName, fileExtension = os.path.splitext(filepath)
            if fileExtension not in file_ext:
                file_ext.append(fileExtension)

    return file_ext  # Self-explanatory.


file_types = get_file_types("d:\\Development\\")
for ty in file_types:
    print ty
Mahmoud Samy
  • 2,822
  • 7
  • 34
  • 78
1

m. samy's answer is correct. However it would be more efficient to use a set which magically check for duplicates.

import os

def get_file_types(directory):
    file_ext = set()
    for root, directories, files in os.walk(directory):
        for filename in files:
            filepath = os.path.join(root, filename)
            fileName, fileExtension = os.path.splitext(filepath)


    return sorted(file_ext)  # Self-explanatory.


file_types = get_file_types("d:\\Development\\")
for ty in file_types:
    print ty
Community
  • 1
  • 1
Xavier Combelle
  • 10,968
  • 5
  • 28
  • 52
0
import os
filesext = []
files = ["porns.txt", "girls.mp3", "hello.png", "folder"]
for file_i in files:
    _, ext = os.path.splitext(file_i)
    if ext:
        filesext.append(ext)
print filesext

>>> ['.txt', '.mp3', '.png']
Victor Martins
  • 739
  • 7
  • 21