1

I am having very basic query that I want to achieve something like this:

<some work>
for f in glob('a1*.txt*'):
    <some work>

I am having a1.txt, a2.txt, a3.txt, ......,a10.txt, a11.txt, ....,a100.txt.

I want to process file like a1.txt ,a2.txt, a3.txt ,...

When I am selecting files in the for loop, it does not appear in a sequence like a1.txt, a2.txt, a3.txt it is appearing like a1.txt, a10.txt ...etc.

How do I resolve this?

a121
  • 798
  • 4
  • 9
  • 20
parag
  • 51
  • 4
  • 1
    Does this answer your question? [Python natural sorting](https://stackoverflow.com/questions/11150239/python-natural-sorting) – kaya3 Feb 08 '21 at 06:55

2 Answers2

0

It is lexically sorted like you would expect strings., natural sort is not provided by Python built-in libraries. You can use external or below mentioned way.

I assume all filenames are same apart from the number part.

>>> sorted(glob.glob('a*.txt*'), key=lambda x: int(os.path.splitext(os.path.basename(x.strip('a')))[0]))
['a1.txt,', 'a2.txt,', 'a10.txt']
>>> 
lllrnr101
  • 2,288
  • 2
  • 4
  • 15
  • Thanks for your help it is working fine o/p ['a1.txt', 'a2.txt', 'a3.txt', 'a10.txt', 'a30.txt'] can you pl explain me the command specially int(os.path.splitext(os.path.basename(x.strip('a')))[0])) – parag Feb 08 '21 at 14:21
-1

As I understand correctly you want just one number after a. To achieve this you can use regex.

import os
import re

def re_glob(pattern, path):
    return filter(re.compile(pattern).match, os.listdir(path))
list(re_glob(r"a.\.txt", "."))

Result:

['a0.txt',
 'a1.txt',
 'a2.txt',
 'a3.txt',
 'a4.txt',
 'a5.txt',
 'a6.txt',
 'a7.txt',
 'a8.txt',
 'a9.txt']
ErdoganOnal
  • 800
  • 6
  • 13