Python: Get list of files in directory sorted by name

In this article, we will discuss different ways to get list of all files in a directory / folder sorted by name in python.

Table of contents

Get list of files in directory sorted by name using glob()

In python, the glob module provides a function glob() to find files in a directory based on matching pattern. Similar to the unix path expansion rules, we can use wildcards and regular expression to match & find few or all files in a directory. We will use this to get a list of all files in a directory and then sort that list of files by name. Steps are as follows,

  1. Get a list of all files or directories in a given directory using glob().
  2. Using the filter() function and os.path.isfileIO(), select files only from the list.
  3. Sort the list of files by name using sorted() function.

Complete example to get a list of all files in directory sorted by name is as follows,

import glob
import os

dir_name = 'C:/Program Files/Java/jdk1.8.0_191/include/'

# Get list of all files in a given directory sorted by name
list_of_files = sorted( filter( os.path.isfile,
                        glob.glob(dir_name + '*') ) )


# Iterate over sorted list of files and print the file paths 
# one by one.
for file_path in list_of_files:
    print(file_path) 

Output:

C:/Program Files/Java/jdk1.8.0_191/include\classfile_constants.h
C:/Program Files/Java/jdk1.8.0_191/include\jawt.h
C:/Program Files/Java/jdk1.8.0_191/include\jdwpTransport.h
C:/Program Files/Java/jdk1.8.0_191/include\jni.h
C:/Program Files/Java/jdk1.8.0_191/include\jvmti.h
C:/Program Files/Java/jdk1.8.0_191/include\jvmticmlr.h

In this solution we created a list of files in a folder using globe() function. Then passed the list to filter() function to select only files from the list and skip dictionaries etc. For this we passed the os.path.isfile() function as an argument to the filter() function. Then we passed the list of files to the sorted() function, which returned a list of files in directory sorted by name.

But the list contains the complete paths of the files. What if we want only file names sorted by names?

Get list of files in directory sorted by names using os.listdir()

In Python, the os module provides a function listdir(dir_path), which returns a list of file and sub-directory names in the given directory path. Then using the filter() function create list of files only. Then sort this list of file names based on the name using the sorted() function.

Complete example to get list of files in directory sorted by name is as follows,

import os

dir_name = 'C:/Program Files/Java/jdk1.8.0_191/include/'

# Get list of all files in a given directory sorted by name
list_of_files = sorted( filter( lambda x: os.path.isfile(os.path.join(dir_name, x)),
                        os.listdir(dir_name) ) )



for file_name in list_of_files:
    print(file_name)

Output:

classfile_constants.h
jawt.h
jdwpTransport.h
jni.h
jvmti.h
jvmticmlr.h

In this solution we created a list of file names in a folder sorted by name.

Python: Get list of files in directory and sub-directories sorted by name

In both the previous examples we created a list of files in a directory sorted by name. But it covered the files in the given directory only, not in nested directories. So, if you want to get a list of all files in directory and sub-directory sorted by name, then checkout this example,

import glob
import os

dir_name = 'C:/Program Files/Java/jdk1.8.0_191/include/'

# Get list of all files in a given directory & sub-directories sorted by name
list_of_files = sorted( filter( os.path.isfile,
                        glob.glob(dir_name + '/**/*', recursive=True) ) )


# Iterate over sorted list of files and print the file paths 
# one by one.
for file_path in list_of_files:
    print(file_path) 

Output:

C:/Program Files/Java/jdk1.8.0_191/include\classfile_constants.h
C:/Program Files/Java/jdk1.8.0_191/include\jawt.h
C:/Program Files/Java/jdk1.8.0_191/include\jdwpTransport.h
C:/Program Files/Java/jdk1.8.0_191/include\jni.h
C:/Program Files/Java/jdk1.8.0_191/include\jvmti.h
C:/Program Files/Java/jdk1.8.0_191/include\jvmticmlr.h
C:/Program Files/Java/jdk1.8.0_191/include\win32\bridge\AccessBridgeCallbacks.h
C:/Program Files/Java/jdk1.8.0_191/include\win32\bridge\AccessBridgeCalls.c
C:/Program Files/Java/jdk1.8.0_191/include\win32\bridge\AccessBridgeCalls.h
C:/Program Files/Java/jdk1.8.0_191/include\win32\bridge\AccessBridgePackages.h
C:/Program Files/Java/jdk1.8.0_191/include\win32\jawt_md.h
C:/Program Files/Java/jdk1.8.0_191/include\win32\jni_md.h

We used the glob() function with pattern ‘/**/*’ and recursive=True argument. It gave a list of all files, sub- directories and files in the sub-directories. Then using the filter() function and os.path.isfile() we filtered the files only and skipped the directories. Then using the sorted() function, sorted these filtered files and created a list of files sorted by name.

Summary:

We learned about different ways to get a list of files in a folder, sorted by name.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top