Using os.walk to return files in subdirectories

Larry Bates larry.bates at websafe.com`
Thu May 22 19:12:33 EDT 2008


dj wrote:
> Hello All,
> 
> I am attempting to us os.walk to populate two lists with values from a
> directory. The first list contains all the files in the directory and
> subdirectories.
> The second list contains only the files in the subdirectories.
> 
> Here is the code:
> 
> import os
> 
> # list of matching files
> allfiles = [] #store all files found
> subfiles = []  # subdir
> 
> for root,dir,files in os.walk("H:/python_experiments", f1):
>      # this list has the files in all directories and subdirectories
>      filelist = [ os.path.join(root,fi) for fi in files if
> fi.endswith(".py") or fi.endswith(".txt") ]
>      for f in filelist:
>          allfiles.append(f)
> 
>      # split the root
>      s= root.split(('\\') or ('\/'))
>      print 's ',
>      print s
> 
>      # assign the last value to end to come to values in dir
>      end= s[-1]
>      print 'end ',
>      print end
>      print '\n'
> 
>      # this list contains only the files in subdirectories
>      for d in dir:
>         if end == d:
>          print 'subdir % s' % (end)
>          sublist = [ os.path.join(root, fi) for fi in files if
> fi.endswith(".py") or fi.endswith(".txt")]
>          for f in sublist:
>              subfiles.append(f)
> 
> for i in subfiles:
>     print 'subfile', i
> 
> 
> for i in allfiles:
>     print "file ", i
> 
> The allfiles list is populated, but the subfiles list is not. Any
> Suggetions ?


I think you were trying to make this harder than it actually is.  Here is a 
tested script that works for me.

import os
basePath = 'c:/Python25'
allfiles = []
subfiles = []

for root, dirs, files in os.walk(basePath):
     for f in files:
         if f.endswith('.txt') or f.endswith('.py'):
             allfiles.append(os.path.join(root, f))
             if root != basePath: # I'm in a subdirectory
                 subfiles.append(os.path.join(root, f))

print "allfiles=", allfiles
print
print "subfiles=", subfiles
print
print "len(allfiles)=", len(allfiles)
print "len(subfiles)=", len(subfiles)


-Larry Bates



More information about the Python-list mailing list