Home>

I want to do the following search with python 3.x.

For example, when the following five files exist, I want to search for a file that does not contain the letter "b" and contains "a".

  • ID1_a.txt
  • ID1_ba.txt
  • ID2_a.txt
  • ID2_ba.txt
  • c.txt
If

file = glob.glob ('* a.txt'), you can get a file path other than c.txt, but what I want to achieve is the two ID1_a.txt and ID2_a.txt Get the file.

  • Answer # 1

    Although often misunderstood,
    The glob argument can be a string containing awildcard, not a regular expression.


    Even if you don't bring up a regular expression, you can check the attribution with the in operator.
    It is unverified, but like this.

    filenames = []
    for filename in glob.iglob ('*'):
        if 'b' in filename:
            continue
        if 'a' in filename:
            filenames.append (filename)

    You can use list comprehensions.

    filenames = [
        filename for filename in glob.iglob ('*')
        if 'b' not in filename and 'a' in filename
    ]

    glob.iglob is used from the viewpoint of saving memory, but there is no problem with glob.glob.

  • Answer # 2

    #-*-coding: utf-8-*-
    from glob import iglob
    for filename in iglob ('ID * _a.txt'):
        print (filename)


    Not like this?

  • Answer # 3

    I wonder if you should use "post-reading statement".

    a = ["ID1_a.txt", "ID1_ba.txt", "ID2_a.txt", "ID2_ba.txt", "c.txt"]
    b = [x for x in a if re.search (r "(?<! b) a.txt", x)]
    print (b)