0

i am using glob.glob('') function to get a list of all files from a folder. I need to get only those names which do not contain '_ab' in it. How is it possible. I understand the need for regular expressions here. i tried (?!....) . what should work here.


example :

x = glob.glob('c:\temp\*ab*')
this returns all files containing ab in it. What should i write here so that it returns only those that do not contain ab in it

Brgds,

kNish

4
Contributors
4
Replies
5
Views
8 Years
Discussion Span
Last Post by ghostdog74
0

i am using glob.glob('') function to get a list of all files from a folder. I need to get only those names which do not contain '_ab' in it. How is it possible. I understand the need for regular expressions here. i tried (?!....) . what should work here.


example :

x = glob.glob('c:\temp\*ab*')
this returns all files containing ab in it. What should i write here so that it returns only those that do not contain ab in it

Brgds,

kNish

You can use a list comprehension to exclude files with '_ab' in the names.

x = [f for f in glob.glob('c:\temp\*.*') if '_ab' not in f]
0

Hi,

thank you for that thought. It worked. It had this change in it.

x = [f for f in glob.glob('e:\\test\\*.*') if '_ab' not in f]
print len(x)


x = [f for f in glob.glob('e:\\test\\*.*') if '_ab' in f]
print len(x)


BRgds,

kNish

0

without glob module, just search for it

import os
os.chdir("/somewhere")
for files in os.listdir("."):
    if not "_ab" in files:
        print files
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.