472,348 Members | 1,219 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,348 software developers and data experts.

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 3713
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: hokiegal99 | last post by:
A few questions about the following code. How would I "wrap" this in a function, and do I need to? Also, how can I make the code smart enough to...
9
by: hokieghal99 | last post by:
This script is not recursive... in order to make it recursive, I have to call it several times (my kludge... hey, it works). I thought os.walk's...
6
by: Dreamcatcher | last post by:
Hello, I'm trying to learn some C, reading my book, Beginning Linux Programming I came across the following program. The program is supposed to...
7
by: KraftDiner | last post by:
The os.walk function walks the operating systems directory tree. This seems to work, but I don't quite understand the tupple that is returned......
8
by: Andre Meyer | last post by:
Hi all os.walk() is a nice generator for performing actions on all files in a directory and subdirectories. However, how can one use os.walk()...
2
by: gregpinero | last post by:
In the example from help(os.walk) it lists this: from os.path import join, getsize for root, dirs, files in walk('python/Lib/email'): print...
0
by: Paul Lemelle | last post by:
Jeff, Thanks for your reply. I would like to like the absolute path of a directory. I thought that os.listdir just returns the nam itself in a...
4
by: Jeff Nyman | last post by:
Greetings all. I did some searching on this but I can't seem to find a specific solution. I have code like this: ...
6
by: D | last post by:
Hello, How can one exclude a directory (and all its subdirectories) when running os.walk()? Thanks, Doug
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.