943,862 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 1534
  • Python RSS
Sep 23rd, 2008
0

list files not containing _ab

Expand 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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
knish is offline Offline
19 posts
since Oct 2007
Sep 23rd, 2008
0

Re: list files not containing _ab

Click to Expand / Collapse  Quote originally posted by knish ...
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.
Python Syntax (Toggle Plain Text)
  1. x = [f for f in glob.glob('c:\temp\*.*') if '_ab' not in f]
Reputation Points: 86
Solved Threads: 40
Junior Poster
solsteel is offline Offline
141 posts
since Mar 2007
Sep 24th, 2008
0

Re: list files not containing _ab

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
knish is offline Offline
19 posts
since Oct 2007
Sep 24th, 2008
0

Re: list files not containing _ab

Is it solved? If yes then don't hesitate to mark it solved!

Have good time with py!
Reputation Points: 462
Solved Threads: 392
Senior Poster
evstevemd is offline Offline
3,681 posts
since Jun 2007
Sep 24th, 2008
0

Re: list files not containing _ab

without glob module, just search for it
Python Syntax (Toggle Plain Text)
  1. import os
  2. os.chdir("/somewhere")
  3. for files in os.listdir("."):
  4. if not "_ab" in files:
  5. print files
Reputation Points: 75
Solved Threads: 44
Junior Poster
ghostdog74 is offline Offline
156 posts
since Apr 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Sizers in Python....
Next Thread in Python Forum Timeline: problems with xmlrpclib and SimpleXMLRPCServer





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC