rt3x.de NoPaste

Sort a list of filenames without leading zeros numerically

This is a nopaste service, powered by Flying Paste.

Author: Malte Bublitz
Language/File type: Python 3

Description

Imagine the following problem:

We have an unsorted list of files returned by Python's glob.glob();and we can assume all files follow a strict pattern:

They start with “rfc”, followed by a number without leading zeros, and the suffix “.pdf”. For example:

rfcs/rfc742.pdf
rfcs/rfc1288.pdf
rfcs/rfc1178.pdf
rfcs/rfc920.pdf
rfcs/rfc236.pdf


Using sorted() we can use a lambda function for extracting the sort key from our values.

Using some split() and replace() magic we can cast the number to int, and also avoid using os.path.basename().

https://docs.python.org/3/library/glob.html#glob.glob
https://docs.python.org/3/library/functions.html#sorted

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
import glob

def sorted_rfcs(glob_expression: str):
	filenames = glob.glob(glob_expression)
	filenames = sorted(
		filenames,
		key=lambda x: int(
            x.split("/")[-1].replace(
            "rfc", "").replace(".pdf","")
        )
	)
	return filenames

for pdf in sorted_rfcs("rfcs/rfc*.pdf"):
	print(pdf)