473,287 Members | 1,800 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 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 3779
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 realize that when a file has 2 or more bad...
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 sole purpose was to recursively walk a directory...
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 walk through directory's and print all its...
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... Can someone explain please? for root, dirs,...
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() for walking through two hierarchies at once? I want...
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 root, "consumes", print sum(), print "bytes in",...
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 data list. I noticed that none was being return...
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: ========================================= def walker1(arg, dirname, names):...
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: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.