Adrian
Adrian

Reputation: 23

How to get python to list all file extensions in the directories?

I have tried this:

ListFiles = list(os.walk(os.getcwd()))
SplitTypes = []

for i in range ( 0 , len(ListFiles) ):
SplitTypes.extend(ListFiles[i].rsplit( "." ) [1])

print(SplitTypes)

but the result I got is this:

['p', 'y', 't', 'x', 't', '3', '2', ' ', 'p', 'm', '4', '0', '1', '2', '2', '4', '1', '4', 's', 'k', 'p', 'm', 'p', '4']

which the numbers are because of the screenshots I had in that directory and the time is separated by '.' and they are separated individually.

I want the result to display:

['py', 'txt', 'skp', 'png']

or something similar to that.

and also this file name 'Screen Shot 2017-05-24 at 11.24.33 am.png' should only show the png bit. After this I will use set() to remove the duplicates.

Upvotes: 2

Views: 7249

Answers (2)

LetzerWille
LetzerWille

Reputation: 5668

Path in pathlib is convenient for getting extensions in Python 3.4+

import os

from pathlib import Path

def find_extensions(dir_path,  excluded = ['', '.txt', '.lnk']):
    import os
    from pathlib import Path    
    extensions = set()
    for _, _, files in os.walk(dir_path):   
        for f in files:
            ext = Path(f).suffix.lower()
            if not ext in excluded:
                extensions.add(ext)
    return extensions 

Upvotes: 0

Abhinav Gupta
Abhinav Gupta

Reputation: 1958

This can be done by:

import os

ListFiles = os.walk(os.getcwd())
SplitTypes = []
for walk_output in ListFiles:
    for file_name in walk_output[-1]:
        SplitTypes.append(file_name.split(".")[-1])

print(SplitTypes)

os.walk gives output as 3 element tuples, out of which the last element is a list of the filenames. So we need to iterate through the output of os.walk, which will give a tuple for files in the current directory and an additional tuple for files in each sub-directory. Then we need to get the last elements of each tuple, which will give us list of file names. We will then, iterate over the filenames, split them using . and extract the extension which will be the last element of the list just produced using split. We can extract the last element of any sequence in Python by subscripting with -1. Lastly, we append the extracted extension to SplitTypes.

If you have no other folders in your folder, then your problem can be easily solved by:

import os

SplitTypes=[]
for file in os.listdir(os.curdir):
    SplitTypes.append(file.split('.')[-1])
print(SplitTypes)

os.listdir(path) gives only the filenames in the path directory.

Upvotes: 4

Related Questions