在glob中使用正则表达式。python的水珠

时间:2022-09-01 23:38:28
import glob

list = glob.glob(r'*abc*.txt') + glob.glob(r'*123*.txt') + glob.glob(r'*a1b*.txt')

for i in list:
  print i

This code is working to list files in current folder which have 'abc' '123' or 'a1b' in their names. How to use one glob to made this function,thanks

这段代码正在处理当前文件夹中的文件,它们的名称中有“abc”“123”或“a1b”。如何使用一个glob来实现这个功能,谢谢?

3 个解决方案

#1


28  

The easiest way would be to filter the glob results yourself. Here is how to do it using a simple loop comprehension:

最简单的方法是你自己过滤一下glob结果。下面是如何使用简单的循环理解:

import glob
res = [f for f in glob.glob("*.txt") if "abc" in f or "123" in f or "a1b" in f]
for f in res:
    print f

You could also use a regexp and no glob:

你也可以使用regexp,而不是glob:

import os
import re
res = [f for f in os.listdir(path) if re.search(r'(abc|123|a1b).*\.txt$', f)]
for f in res:
    print f

(By the way, naming a variable list is a bad idea since list is a Python type...)

(顺便说一下,命名变量列表是个坏主意,因为列表是Python类型的…)

#2


10  

Here is a ready to use way of doing this, based on the other answers. It's not the most performance critical, but it works as described;

这里有一个可以使用的方法,基于其他的答案。这不是最关键的性能,但是它可以像描述的那样工作;

def reglob(path, exp, invert=False):
    """glob.glob() style searching which uses regex

    :param exp: Regex expression for filename
    :param invert: Invert match to non matching files
    """

    m = re.compile(exp)

    if invert is False:
        res = [f for f in os.listdir(path) if m.search(f)]
    else:
        res = [f for f in os.listdir(path) if not m.search(f)]

    res = map(lambda x: "%s/%s" % ( path, x, ), res)
    return res

#3


0  

for filename in glob.iglob(path_to_directory + "*.txt"):
    if filename.find("abc") != -1 or filename.find("123") != -1 or filename.find("a1b") != -1:
        print filename

#1


28  

The easiest way would be to filter the glob results yourself. Here is how to do it using a simple loop comprehension:

最简单的方法是你自己过滤一下glob结果。下面是如何使用简单的循环理解:

import glob
res = [f for f in glob.glob("*.txt") if "abc" in f or "123" in f or "a1b" in f]
for f in res:
    print f

You could also use a regexp and no glob:

你也可以使用regexp,而不是glob:

import os
import re
res = [f for f in os.listdir(path) if re.search(r'(abc|123|a1b).*\.txt$', f)]
for f in res:
    print f

(By the way, naming a variable list is a bad idea since list is a Python type...)

(顺便说一下,命名变量列表是个坏主意,因为列表是Python类型的…)

#2


10  

Here is a ready to use way of doing this, based on the other answers. It's not the most performance critical, but it works as described;

这里有一个可以使用的方法,基于其他的答案。这不是最关键的性能,但是它可以像描述的那样工作;

def reglob(path, exp, invert=False):
    """glob.glob() style searching which uses regex

    :param exp: Regex expression for filename
    :param invert: Invert match to non matching files
    """

    m = re.compile(exp)

    if invert is False:
        res = [f for f in os.listdir(path) if m.search(f)]
    else:
        res = [f for f in os.listdir(path) if not m.search(f)]

    res = map(lambda x: "%s/%s" % ( path, x, ), res)
    return res

#3


0  

for filename in glob.iglob(path_to_directory + "*.txt"):
    if filename.find("abc") != -1 or filename.find("123") != -1 or filename.find("a1b") != -1:
        print filename