Re: Using os.walk to return files in subdirectories

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dj

    Re: Using os.walk to return files in subdirectories

    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_experime nts", f1):
    # this list has the files in all directories and subdirectories
    filelist = [ os.path.join(ro ot,fi) for fi in files if
    fi.endswith(".p y") or fi.endswith(".t xt") ]
    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(ro ot, fi) for fi in files if
    fi.endswith(".p y") or fi.endswith(".t xt")]
    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 ?
  • Chris Hulan

    #2
    Re: Using os.walk to return files in subdirectories

    On May 22, 5:24 pm, dj <d.a.aberna...@ gmail.comwrote:
    ....snip...
    for d in dir:
    if end == d:
    dir is the list of subdirs of the current dir, and the current dir
    is NOT a subdir of itself so end == dir is never true

    Are you trying to get a list per subdir?

    cheers

    Comment

    • Larry Bates

      #3
      Re: Using os.walk to return files in subdirectories

      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_experime nts", f1):
      # this list has the files in all directories and subdirectories
      filelist = [ os.path.join(ro ot,fi) for fi in files if
      fi.endswith(".p y") or fi.endswith(".t xt") ]
      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(ro ot, fi) for fi in files if
      fi.endswith(".p y") or fi.endswith(".t xt")]
      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(basePat h):
      for f in files:
      if f.endswith('.tx t') or f.endswith('.py '):
      allfiles.append (os.path.join(r oot, f))
      if root != basePath: # I'm in a subdirectory
      subfiles.append (os.path.join(r oot, f))

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


      -Larry Bates

      Comment

      Working...