简体   繁体   English

如何对文件名为python的文件夹中的文件进行排序

[英]How to sort files in a folder with file name in python

I am trying to sort some simulation files with the extension.sim.我正在尝试对一些扩展名为 .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', ...]
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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 python 中的 file.txt 中的文件名重命名文件夹中的文件? - how to rename files in a folder with the files name in file.txt in python? 如何访问 python 中文件名中具有相同字符串的文件夹中的文件? - How to access files in folder with same string in file name in python? 如何使用 python 对文件夹中的文件进行排序? - How to sort files in a folder using python? 如何在python中获取文件夹名称和文件名 - how to get a folder name and file name in python 如何将文件夹中存在的 .mp4 文件与 .csv 文件中的名称相匹配,并在 python 中根据某些列值进行排序? - How to match the .mp4 files present in a folder with the names in .csv file and sort according to some column value, in python? 如何在python的文件夹中保存同名文件? - How to save files with same name in folder in python? 考虑到文件名包含 python 中目标文件夹的名称,如何将文件从一个文件夹复制到另一个文件夹 - How to copy files from one folder to another taking into account that the file name contains the name of the destination folders in python Python 按文件名排序 - Python sort by file name 根据python中的名称排序文件 - sort files according to name in python 如何使用 python 用父文件的名称加上一些索引重命名文件夹中的子文件 - How to rename the sub files in a folder with the name of the parent file plus some index using python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM