469,900 Members | 1,714 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,900 developers. It's quick & easy.

Re: Using os.walk to return files in subdirectories

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
2 3608
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
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.

Similar topics

5 posts views Thread by hokiegal99 | last post: by
9 posts views Thread by hokieghal99 | last post: by
6 posts views Thread by Dreamcatcher | last post: by
7 posts views Thread by KraftDiner | last post: by
8 posts views Thread by Andre Meyer | last post: by
2 posts views Thread by gregpinero | last post: by
reply views Thread by Paul Lemelle | last post: by
4 posts views Thread by Jeff Nyman | last post: by
6 posts views Thread by D | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.