Set order using glob.glob
Solution 1
You can use sorted(list)
to sort your file names before iterating over them:
#!/usr/bin/env python
import sys, os, glob
def sorter(item):
"""Get an item from the list (one-by-one) and return a score for that item."""
return item[1]
files = sorted(glob.glob('*.txt'), key=sorter)
for file in files:
print(file)
Here, it sorts by the second letter in the file name. Change the sorter()
function to how you want to sort your file list.
To sort alphabetically, you don't need the key=sorter
part, because that's the default behavior of sorted()
with a list of strings. So then it would become:
files = sorted(glob.glob('*.txt'))
for file in files:
print(file)
Solution 2
You can sort the entries from glob. You may use the default sorting or choose an algorithm of your own:
Simple usage:
#! /usr/bin/env python
import sys, os, glob
for file in sorted(glob.glob('*.txt')):
print(file)
'sorted' manual: https://python-reference.readthedocs.io/en/latest/docs/functions/sorted.html
Related videos on Youtube
gusa10
Updated on June 04, 2022Comments
-
gusa10 almost 2 years
I want to set my own order when my script open files but glob.glob default for opening files is random.
I have the following files: 'fish.txt', 'expo.txt', 'random.txt'.
This is a small-scale example of all of my files, I want to set my order.
I have the written the normal way to open files with glob.glob
#! /usr/bin/env python import sys, os, glob mylist = ['fish.txt','random.txt', 'expo.txt'] def sorter(item): for item in mylist: return item for file in sorted(glob.glob('*.txt'), key = sorter): print(file)
My desired output would be:
fish.txt
random.txt
expo.txt
-
iz_ over 5 yearsHow do you want the order to be set? Sorted?
-
-
BlackBear over 5 yearswhy do you sort on the second character..?
-
user8181134 over 5 yearsIt was just an example of what's possible. You should change the
sorter()
function to an algoritthm how you want to sort the files. If you just want to sort alphabetically, you can ommit thekey=sorter
part. -
gusa10 over 5 years@BlackBear , thank you! . Can you help me to complete the script? I have this ( It does not work): def sorter(item): for item in mylist: return item. I created mylist [] in the order I want
-
user8181134 over 5 yearsThe
sorter()
function receives one item (one filename in this case) at each call, not the whole list. So to sort alphabetically, it would just return the filename, which is why you can omit thekey=sorter
from thesorted(...)
function call then. -
gusa10 over 5 years@user8181134 I do not want to sort alphabetically but I want to input my own order. That is why I create myList = [] in my expected order. My idea was that if I read each value of myList, each value will be read in the same order as they appear in the list.
-
user8181134 over 5 yearsFor a custom sorting algorithm, your
sorter()
function should return a number as a score for the item. The item with the lowest score is placed in the first spot in the list -
Sharad over 5 yearsWhat's the logic you want for ordering? You need to implement that logic in 'def sorter()' above - it's currently returning items in the same order it received.
-
gusa10 over 5 yearsthanks!, my question is more related about setting my own order based on the order of a list I create. make sense?