I have a directory structure that resembles the following:
Dir1
Dir2
Dir3
Dir4
L SubDir4.1
L SubDir4.2
L SubDir4.3
I want to generate a list of files (with full paths) that include all the contents of Dirs1-3
, but only SubDir4.2
inside Dir4
. The code I have so far is
import fnmatch
import os
for root, dirs, files in os.walk( '.' )
if 'Dir4' in dirs:
if not 'SubDir4.2' in 'Dir4':
dirs.remove( 'Dir4' )
for file in files
print os.path.join( root, file )
My problem is that the part where I attempt to exclude any file that does not have SubDir4.2
in it's path is excluding everything in Dir4
, including the things I would like to remain. How should I amend that above to to do what I desire?
Update 1: I should add that there are a lot of directories below Dir4
so manually listing them in an excludes list isn't a practical option. I'd like to be able to specify SubDur4.2
as the only subdirectory within Dir4
to be read.
Update 2: For reason outside of my control, I only have access to Python version 2.4.3.