list files not containing _ab

Reply

Join Date: Oct 2007
Posts: 18
Reputation: knish is an unknown quantity at this point 
Solved Threads: 0
knish knish is offline Offline
Newbie Poster

list files not containing _ab

 
0
  #1
Sep 23rd, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 110
Reputation: solsteel is on a distinguished road 
Solved Threads: 31
solsteel solsteel is offline Offline
Junior Poster

Re: list files not containing _ab

 
0
  #2
Sep 23rd, 2008
Originally Posted by knish View Post
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.
  1. x = [f for f in glob.glob('c:\temp\*.*') if '_ab' not in f]
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 18
Reputation: knish is an unknown quantity at this point 
Solved Threads: 0
knish knish is offline Offline
Newbie Poster

Re: list files not containing _ab

 
0
  #3
Sep 24th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,356
Reputation: evstevemd has a spectacular aura about evstevemd has a spectacular aura about evstevemd has a spectacular aura about 
Solved Threads: 125
evstevemd's Avatar
evstevemd evstevemd is offline Offline
Nearly a Posting Virtuoso

Re: list files not containing _ab

 
0
  #4
Sep 24th, 2008
Is it solved? If yes then don't hesitate to mark it solved!

Have good time with py!
Atheist: God is man made imagination, he doesn't exist!
Theist: It's okay, can you imagine anything else that doesn't exist?
Junior MD --- Python, C++ and PHP
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 148
Reputation: ghostdog74 is on a distinguished road 
Solved Threads: 40
ghostdog74 ghostdog74 is offline Offline
Junior Poster

Re: list files not containing _ab

 
0
  #5
Sep 24th, 2008
without glob module, just search for it
  1. import os
  2. os.chdir("/somewhere")
  3. for files in os.listdir("."):
  4. if not "_ab" in files:
  5. print files
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC