By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,551 Members | 1,127 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,551 IT Pros & Developers. It's quick & easy.

Re: Using os.walk to return files in subdirectories

P: n/a
dj
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 ?
Jun 27 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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
Jun 27 '08 #2

P: n/a
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
Jun 27 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.