83jahs8
83jahs8

Reputation: 63

python sort on extension / Name of the file

I'm working on a script for a school project:

search_folder = raw_input('Choose Directory')

for root, dirs, files in os.walk(search_folder):
       for file in files:
               pathname = os.path.join(root, file)
               print pathname
               print os.path.getsize(pathname)

How can I sort the output on Extension?

Upvotes: 1

Views: 2530

Answers (1)

tmthydvnprt
tmthydvnprt

Reputation: 10748

Sorting filenames

Use sorted() with an os.path.splitext() lambda in the sort-by key parameter.

Example

import os

search_folder = '/Users/temp/'

results = []
for root, dirs, files in os.walk(search_folder):
       for f in files:
            results.append(os.path.join(root, f))

sorted_results = sorted(results, key=lambda x: os.path.splitext(x)[1])

for path in sorted_results:
    print path
    print os.path.getsize(path)

/Users/temp/.DS_Store
6148
/Users/temp/.localized
0
/Users/temp/profile.png
81168
/Users/temp/IMG_0115.xcf
113212
/Users/temp/profile.xcf
535202

Other considerations & suggestions

Using list comprehension

You can also use list comprehension (for speed and readability) to walk the files:

results = [os.path.join(root, f) 
               for root, dirs, files in os.walk(search_folder) 
                   for f in files]

Memory efficient sorting

Sort without storing suggestion from @PM2Ring:

for root, dirs, files in os.walk(search_folder):
    for f in sorted(files, key=lambda x: tuple(reversed(os.path.splitext(x)))):
        pathname = os.path.join(root, f)
        print pathname 
        print os.path.getsize(pathname)

Upvotes: 2

Related Questions