How to sort files in Linux by number?

My program reads the number processed by the filename. I want to be ordered exactly like this, but in this example the list could be named 500, 4000, 7000. What should the naming convention look like for this? That is, when I increment the numbers, it lists it from lowest to highest.

I really want it to sort by rank (which starts at zero here), then sort it in ascending numbers .. 500, 5000, 7000.

enter image description here

DESIRED OUTPUT

LOG-rank-0-die-10-delay-500.txt
LOG-rank-0-die-10-delay-4000.txt
LOG-rank-0-die-10-delay-7000.txt
LOG-rank-1-die-10-delay-500.txt
LOG-rank-1-die-10-delay-4000.txt
LOG-rank-1-die-10-delay-7000.txt
LOG-rank-2-die-10-delay-500.txt
LOG-rank-2-die-10-delay-4000.txt
LOG-rank-2-die-10-delay-7000.txt

      

Corresponding code

for filenamelogs in sorted(os.listdir(log_directory)):
            for each_line in filenamelogs:
                   #various file parsing activity

      

I am adding data files by file to various arrays. Unfortunately for me it's terrible if I can't sort the files in the requested order. Perhaps my question boils down to developing a custom method to read in files under the sorting constraints I provide.

+3


source to share


1 answer


From a comment on a blog linked at blog :

>>> import re
>>> def sort_nicely(l):
...     """
...     Sort the given list in the way that humans expect. Modifies the original list.
...     """
...     convert = lambda text: int(text) if text.isdigit() else text
...     alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)]
...     l.sort(key=alphanum_key)
...
>>> mylist = ['LOG-rank-0-die-10-delay-500.txt',
... 'LOG-rank-0-die-10-delay-4000.txt',
... 'LOG-rank-0-die-10-delay-7000.txt',
... 'LOG-rank-1-die-10-delay-500.txt',
... 'LOG-rank-1-die-10-delay-4000.txt',
... 'LOG-rank-1-die-10-delay-7000.txt',
... 'LOG-rank-2-die-10-delay-500.txt',
... 'LOG-rank-2-die-10-delay-4000.txt',
... 'LOG-rank-2-die-10-delay-7000.txt']
>>> sort_nicely(mylist)
>>> print(*mylist, sep='\n')
LOG-rank-0-die-10-delay-500.txt
LOG-rank-0-die-10-delay-4000.txt
LOG-rank-0-die-10-delay-7000.txt
LOG-rank-1-die-10-delay-500.txt
LOG-rank-1-die-10-delay-4000.txt
LOG-rank-1-die-10-delay-7000.txt
LOG-rank-2-die-10-delay-500.txt
LOG-rank-2-die-10-delay-4000.txt
LOG-rank-2-die-10-delay-7000.txt

      



Into a return

new one, sorted list

instead of changing the original:

>>> def sort_nicely(l):
...     """
...     Sort the given list in the way that humans expect. Returns a new list.
...     """
...     convert = lambda text: int(text) if text.isdigit() else text
...     alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)]
...     return sorted(l, key=alphanum_key)
...
>>> newlist = sort_nicely(mylist)
>>> print(*newlist, sep='\n')

      

+5


source







All Articles