Using os.walk to return files in subdirectories
dj
d.a.abernathy at gmail.com
Thu May 22 17:24:11 EDT 2008
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 ?
More information about the Python-list
mailing list