os.walk() usage

R

rbt

I'm trying to write very small, modular code as functions to break up a
big monolithic script that does a file system search for particular
strings. The script works well, but it's not easy to maintain or add
features to.

I'd like to have a function that builds a list of files with os.walk()
and then have other functions accept that list as a parameter and modify
it as needed. For example, if the user has specified that certain files
and folders be excluded from the walk, I'd like to have functions like this:

def build_list(path):
fs_list = os.walk(path)
return fs_list

def skip_files(fs_list):
remove files
return fs_list

def skip_dirs(fs_list):
remove dirs
return fs_list

def search(fs_list):
pass

The problem I'm encountering is passing the list to other functions.
It's almost as if each function needs to build the list itself (walk the
filesystem)... which gets in the way of what I was asked to do (break
this thing up into modular, maintainable pieces).

Any tips on this?

Thanks
 
W

wittempj

every object in os.walk() returns a 3-tuple, like below, it seems your
code assumes it returns only a list of files.

for d in os.walk('c:\\temp'):
(dirpath, dirnames, filenames) = d
print dirpath
print dirnames
print filenames
 
B

bruno modulix

rbt said:
I'm trying to write very small, modular code as functions to break up a
big monolithic script that does a file system search for particular
strings. The script works well, but it's not easy to maintain or add
features to.

I'd like to have a function that builds a list of files with os.walk()
and then have other functions accept that list as a parameter and modify
it as needed. For example, if the user has specified that certain files
and folders be excluded from the walk, I'd like to have functions like
this:

def build_list(path):
fs_list = os.walk(path)
return fs_list

def skip_files(fs_list):
remove files
return fs_list

def skip_dirs(fs_list):
remove dirs
return fs_list

def search(fs_list):
pass

The problem I'm encountering is passing the list to other functions.

err...

search(skip_files(skip_dirs(build_list(path)))) ?

What's your problem *exactly* ?-)

BTW, you may want to have a look at the path module :
http://www.jorendorff.com/articles/python/path/
 
R

rbt

every object in os.walk() returns a 3-tuple, like below, it seems your
code assumes it returns only a list of files.

for d in os.walk('c:\\temp'):
(dirpath, dirnames, filenames) = d
print dirpath
print dirnames
print filenames

Thank you, this fixed it. I didn't read the docs well enough ;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

os.walk/list 3
using os.walk to generate objects 0
os.walk() 5
os.walk Value Error? 3
Multi-line commands with 'python -c' 11
postprocessing in os.walk 7
How does os.walk work? 2
os.walk trouble 6

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
VetaMcRae
Top