繁体   English   中英

文件名与fnmatch匹配

[英]File name matching with fnmatch

我有一个文件目录,格式为: LnLnnnnLnnn.txt

其中L =字母,n =数字。 例如: p2c0789c001.txt

我想根据第二个数字(即0789)是否在特定的数字序列(例如0001到0146)内来分离这些文件。

使用fnmatch有一个简单的方法吗? 或者我应该使用正则表达式?

这是我到目前为止的代码:

out_files = []
for root, dirs, filenames in os.walk('.'):
   for filename in fnmatch.filter(filenames, '???[0-9][0-9][0-9][0-9]????*.txt'):
       out_files.append(os.path.join(root, filename))

你不能在fnmatch.filter()轻松fnmatch.filter() ,但你可以自己做:

out_files = []
for root, dirs, filenames in os.walk('.'):
   for filename in fnmatch.filter(filenames, '???[0-9][0-9][0-9][0-9]????*.txt'):
       if(1 <= int(filename[3:7]) <= 146):
           out_files.append(os.path.join(root, filename))

或者,对于列表理解粉丝:

import os
import fnmatch
out_files = [os.path.join(root, filename)
             for root, dirs, filenames in os.walk('.')
             for filename in fnmatch.filter(filenames,
                                            '???[0-9][0-9][0-9][0-9]????*.txt')
             if 1 <= int(filename[3:7]) <= 146]

编辑 :哎呀,忘了一个额外的循环。 另外,看看这是否有更好的性能。

EDIT2 :如果第一个字母是c ,则检查倒数第二个元素,该元素基于两个备选方案的标准,保证存在。

out_files = []
for root, dirs, filenames in os.walk('.'):
    for filename in filesnames:
        try:
            if  1 <= int(filename.split('c')[-2]) <= 146:
                out_files.append(...)
        except IndexError:
            continue

或者,使用发电机:

out_files = []
for root, dirs, filenames in os.walk('.'):
    for filename in (name for name in filenames if 'c' in name):
        if  1 <= int(filename.split('c')[-2]) <= 146:
            out_files.append(...)

如果字符串开头有其他c's或数字更改前的字符串长度:

if 1 <= int(re.findall(r"c([0-9]+)c", s)[0]) <= 487

或者,如果总有四位数字:

if 1 <= int(re.findall(r"c(\d{4})c", s)[0]) <= 487:

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM