path stuff

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Thu May 10 21:08:48 EDT 2007


En Thu, 10 May 2007 19:04:30 -0300, fscked <fsckedagain at gmail.com>  
escribió:

> ok, I lied, it is still doing the archived folders. Here is the code:
>
> import os, sys
> from path import path
>
> myfile = open("boxids.txt", "r", 0)
> for line in myfile:
>     d = 'D:\\Dir\\' + path(line.strip())
>     for f in d.walkfiles('*Config*.xml'):
>         for dirpath, dirnames, filenames in os.walk(d):
>             if "Archived" in dirnames:
>                 dirnames.remove("Archived") #skip this directory
>                 print f
> print 'Done'
>
>
> when it does the print f it still shows me the dirs i don't want to
> see.

You are walking the directory structure *twice*, using two different  
methods at the same time. Also, there is no standard `path` module, and  
several implementations around, so it would be a good idea to tell us  
which one you use.
If you want to omit a directory, and include just filenames matching a  
pattern:

import os, sys, os.path, fnmatch

def findinterestingfiles(root_dir):
   for dirpath, dirnames, filenames in os.walk(root_dir):
     if "Archived" in dirnames:
       dirnames.remove("Archived")
     for filename in fnmatch.filter(filenames, '*Config*.xml'):
       fullfn = os.path.join(dirpath, filename)
       print fullfn

myfile = open("boxids.txt", "r")
for line in myfile:
   dirname = os.path.join('D:\\Dir\\', line.strip())
   findinterestingfiles(dirname):
myfile.close()

-- 
Gabriel Genellina




More information about the Python-list mailing list