When using glob.glob
in Python you might experience some unexpected results. Lets say you have the following directory content.
andre@Users-MBP:~/dev/globdemo% ls -gnGl
total 0
-rw-r--r-- 1 20 0 Feb 7 21:51 2019-02-05-demo1.md
-rw-r--r-- 1 20 0 Feb 7 21:51 2019-02-06-demo2.md
-rw-r--r-- 1 20 0 Feb 7 21:51 2019-02-07-demo3.md
Using the following simple program will create some unexpected output at least at a first glance.
import os, glob
path = '/Users/Andre/dev/globdemo'
for file in glob.glob( os.path.join(path, '*.*')):
print(file)
This will print the following:
/Users/Andre/dev/globdemo/2019-02-06-demo2.md
/Users/Andre/dev/globdemo/2019-02-07-demo3.md
/Users/Andre/dev/globdemo/2019-02-05-demo1.md
As you can see the order of files is different from the one using ls
. This is because glob.glob
is basically just a wrapper for os.listdir
which returns the directory contents in arbitrary order. On MacOS you will get the same order for your output when using -f
.
andre@Users-MBP:~/dev/globdemo% ls -gnGfl
total 0
drwxr-xr-x 5 20 160 Feb 7 21:51 .
drwxr-xr-x 21 20 672 Feb 7 21:58 ..
-rw-r--r-- 1 20 0 Feb 7 21:51 2019-02-06-demo2.md
-rw-r--r-- 1 20 0 Feb 7 21:51 2019-02-07-demo3.md
-rw-r--r-- 1 20 0 Feb 7 21:51 2019-02-05-demo1.md
If you need your files sorted in python you can simply solve this by using sorted
in your program.
import os, glob
path = '/Users/Andre/dev/globdemo'
for file in sorted(glob.glob( os.path.join(path, '*.*'))):
print(file)
Running this slightly modified program will create the following output:
andre@Users-MBP:~/dev% python3 globdemosorted.py
/Users/Andre/dev/globdemo/2019-02-05-demo1.md
/Users/Andre/dev/globdemo/2019-02-06-demo2.md
/Users/Andre/dev/globdemo/2019-02-07-demo3.md