-2

I am trying to sort some simulation files with the extension .sim. At the moment I have the following code:

import os
import re


files = [f for f in os.listdir('.') if re.match(r'.*\.sim', f)]

print(files) 

When I run the code I get these results:

['Yunlin_Shorepull_South_Current_1.8_Wind_0.sim', 
'Yunlin_Shorepull_South_Current_1.8_Wind_0_Relocated.sim', 
'Yunlin_Shorepull_South_Current_1.8_Wind_1.sim', 
'Yunlin_Shorepull_South_Current_1.8_Wind_10.sim', 
'Yunlin_Shorepull_South_Current_1.8_Wind_10_Relocated.sim', 
'Yunlin_Shorepull_South_Current_1.8_Wind_11.sim', ...]
dspencer
  • 4,297
  • 4
  • 22
  • 43
  • Please post code and not images – Be Chiller Too Jul 17 '20 at 10:04
  • 1
    Does this answer your question? [How to sort a list of strings?](https://stackoverflow.com/questions/36139/how-to-sort-a-list-of-strings) – Be Chiller Too Jul 17 '20 at 10:05
  • How could I possibly sort the files by the file name and some numbers in the file name? – Araz Pishdad Jul 17 '20 at 10:06
  • This is the result of running the code above: ['Yunlin_Shorepull_South_Current_1.8_Wind_0.sim', 'Yunlin_Shorepull_South_Current_1.8_Wind_0_Relocated.sim', 'Yunlin_Shorepull_South_Current_1.8_Wind_1.sim', 'Yunlin_Shorepull_South_Current_1.8_Wind_10.sim', 'Yunlin_Shorepull_South_Current_1.8_Wind_10_Relocated.sim', 'Yunlin_Shorepull_South_Current_1.8_Wind_11.sim', ... – Araz Pishdad Jul 17 '20 at 13:25
  • Yes. They are sorted. – Be Chiller Too Jul 17 '20 at 13:35
  • I would like to sort them according to their names and the numbers after Wind_. – Araz Pishdad Jul 17 '20 at 13:54

1 Answers1

0
import os
import re

files = [f for f in os.listdir('.') if f.endswith(".sim")]
files = ['Prefix_0.sim',
         'Prefix_1.sim',
         'Prefix_0_Relocated.sim',
         'Prefix_10.sim',
         'Prefix_11.sim',
         'Prefix_1_Relocated.sim',
         'Prefix_2_Relocated.sim',
         'Prefix_2.sim',
         'Prefix_10_Relocated.sim',
         'Prefix_12.sim',
         'Prefix_12_Relocated.sim',
         'Prefix_11_Relocated.sim',
         ]

prefix = "Prefix_"
suffix = "_Relocated.sim"
number_regex = prefix + r"(\d+)"

def extract_number(s):
    match = re.match(number_regex, s)
    return int(match.group(1))

original_files = [f for f in files if not f.endswith(suffix)]
relocated_files = [f for f in files if f.endswith(suffix)]

sorted_files = sorted(original_files, key=extract_number) + \
               sorted(relocated_files, key=extract_number)

for f in sorted_files:
    print(f)

Prefix_0.sim
Prefix_1.sim
Prefix_2.sim
Prefix_10.sim
Prefix_11.sim
Prefix_12.sim
Prefix_0_Relocated.sim
Prefix_1_Relocated.sim
Prefix_2_Relocated.sim
Prefix_10_Relocated.sim
Prefix_11_Relocated.sim
Prefix_12_Relocated.sim
Be Chiller Too
  • 2,502
  • 2
  • 16
  • 42
  • I would like the output to look like this: – Araz Pishdad Jul 17 '20 at 14:14
  • I would like the output to look like this: Yunlin_Shorepull_South_Current_1.8_Wind_0.sim Yunlin_Shorepull_South_Current_1.8_Wind_1.sim Yunlin_Shorepull_South_Current_1.8_Wind_2.sim Yunlin_Shorepull_South_Current_1.8_Wind_3.sim... Yunlin_Shorepull_South_Current_1.8_Wind_0_Relocated.sim Yunlin_Shorepull_South_Current_1.8_Wind_1_Relocated.sim Yunlin_Shorepull_South_Current_1.8_Wind_2_Relocated.sim Yunlin_Shorepull_South_Current_1.8_Wind_3_Relocated.sim... – Araz Pishdad Jul 17 '20 at 14:49
  • After running the modified code I get the following error: AttributeError: 'NoneType' object has no attribute 'group' – Araz Pishdad Jul 21 '20 at 11:26