LoginSignup
13
13

More than 5 years have passed since last update.

Python globで得るファイル名を数字順にする

Posted at

環境によっては何もしなくてもglob.globで得られるファイルは数字順であったが、他の環境ではそうでもなくarbitraryであった。

出典:http://stackoverflow.com/questions/12093940/reading-files-in-a-particular-order-in-python
を参考にsortすることで解決する。

import glob 
import re
def numericalSort(value):
    numbers = re.compile(r'(\d+)')
    parts = numbers.split(value)
    parts[1::2] = map(int, parts[1::2])
    return parts
fitsf=sorted(glob.glob('*.fits'), key=numericalSort)
13
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
13