Apparently, it's order in the filesystem, whatever that may be; the order they were written to disk? But for two globs of files, they are by no means guaranteed to have the same matched ordering.
Python glob. glob() method returns a list of files or folders that matches the path specified in the pathname argument. This function takes two arguments, namely pathname, and recursive flag. pathname : Absolute (with full path and the file name) or relative (with UNIX shell-style wildcards).
Glob in Python. glob is a powerful tool in Python to help with file management and filtering. While os helps manage and create specific paths that are friendly to whatever machine they are used on, glob helps to filter through large datasets and pull out only files that are of interest.
Order is arbitrary, but you can sort them yourself
If you want sorted by name:
sorted(glob.glob('*.png'))
sorted by modification time:
import os
sorted(glob.glob('*.png'), key=os.path.getmtime)
sorted by size:
import os
sorted(glob.glob('*.png'), key=os.path.getsize)
etc.
It is probably not sorted at all and uses the order at which entries appear in the filesystem, i.e. the one you get when using ls -U
. (At least on my machine this produces the same order as listing glob
matches).
By checking the source code of glob.glob
you see that it internally calls os.listdir
, described here:
http://docs.python.org/library/os.html?highlight=os.listdir#os.listdir
Key sentence: os.listdir(path) Return a list containing the names of the entries in the directory given by path. The list is in arbitrary order. It does not include the special entries '.' and '..' even if they are present in the directory.
Arbitrary order. :)
Order is arbitrary, but there are several ways to sort them. One of them is as following:
#First, get the files:
import glob
import re
files =glob.glob1(img_folder,'*'+output_image_format)
# if you want sort files according to the digits included in the filename, you can do as following:
files = sorted(files, key=lambda x:float(re.findall("(\d+)",x)[0]))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With