-2

I have a directory that contains directories like /sample1, /sample10, /sample11 etc. when I am using os.walk to access all of them one by one, I am facing some difficulties.

for root, dirs, files in os.walk(mypath):
    if 'sample1' in root:
       print root

now, this returns all root /sample1, /sample10, /sample11 etc. as 'sample1' is there for all directories. how can I obtain only sample1 directory ?

may be regex would be good but I dont know how to use it.

Wooble
  • 80,189
  • 12
  • 97
  • 124
Dolly
  • 1
  • If you are at the very beginning with python, you should always study some elementary language features here: http://docs.python.org/library/index.html – heltonbiker Oct 19 '11 at 18:00

1 Answers1

1

Test for equality: if root == "sample1": …

David Wolever
  • 130,273
  • 78
  • 311
  • 472