Sort the file names obtained by Python glob in numerical order

In some environments the files obtained with glob.glob were in numerical order without doing anything, but in other environments they were not so arbitrary.

Source: http://stackoverflow.com/questions/12093940/reading-files-in-a-particular-order-in-python It is solved by sorting with reference to.

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)

Recommended Posts

Sort the file names obtained by Python glob in numerical order
Read the file line by line in Python
Read the file line by line in Python
A memo organized by renaming the file names in the folder with python
Download the file in Python
Sort by date in python
Sort tuple list in Python by specifying the ascending / descending order of multiple keys
[Python] Open the csv file in the folder specified by pandas
[Python] Read the specified line in the file
How to sort by specifying a column in the Python Numpy array.
[Python] Sort the list of pathlib.Path in natural sort
[Python] Sort the table by sort_values (pandas DataFrame)
Sort list elements in a specified order in Python
Download the file while viewing the progress in Python 3.x
File operations in Python
Sort in Python. Next, let's think about the algorithm.
File processing in Python
Get the file name in a folder using glob
Sort files updated within the period specified by the find command in order of size
Bubble sort in Python
Log in by passing the login session cookie obtained by Python requests to the Selenium browser.
File operations in Python
Shift the alphabet string by N characters in Python
Store the stock price scraped by Python in the DB
Custom sort in Python3
[Work efficiency] How to change file names in Python
[Python] How to output the list values in order
Natural order in python
Python> Get a list of files in multiple directories> Use glob | Sort by modification time
How to switch the configuration file to be read by Python
Get the MIME type in Python and determine the file format
Sort and output the elements in the list as elements and multiples in Python.
Sort the string array in order of length & Japanese syllabary
[Note] Import of a file in the parent directory in Python
After enabling the python virtual environment in the batch file, run the python file
Visualize the correlation matrix by principal component analysis in Python
Google search for the last line of the file in Python
Implemented the algorithm of "Algorithm Picture Book" in Python3 (selection sort)
Find the difference in Python
Naturally sort Path in Python
Python basic dict sort order
python in mongodb in descending sort
Sort by file modification date
If I thought I didn't see the pyc file recently, it was quarantined in pycache by python3.
Process the contents of the file in order with a shell script
[Understanding in the figure] Management of Python virtual environment by Pipenv
Check if the password hash generated by PHP matches in Python
Read the standard output of a subprocess line by line in Python
[pepper] Pass the JSON data obtained by python request to the tablet.
[python] Get the rank of the values in List in ascending / descending order
Read a file in Python with a relative path from the program
[Python] Do not put Japanese in the path used by OpenCV
Get the formula in an excel file as a string in Python
Play by hitting the Riot Games API in Python First half
Trial of writing the configuration file in Python instead of .ini etc.
I tried to open the latest data of the Excel file managed by date in the folder with Python
Extract the xz file with python
sort warning in the pd.concat function
Getting the arXiv API in Python
File / folder path manipulation in Python
[Python] Sort iterable by multiple conditions