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 3 2579
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
rbt wrote: 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/
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])" wi******@hotmail.com wrote: 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 ;) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Marcello Pietrobon |
last post by:
Hello,
I am using Pyton 2.3
I desire to walk a directory without recursion
this only partly works:
def walk_files() :
for root, dirs, files in os.walk(top, topdown=True):
for filename in...
|
by: rbt |
last post by:
More of an OS question than a Python question, but it is Python related
so here goes:
When I do os.walk('/') on a Linux computer, the entire file system is
walked. On windows, however, I can...
|
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,...
|
by: Bruce |
last post by:
Hi all,
I have a question about traversing file systems, and could use some
help. Because of directories with many files in them, os.walk appears
to be rather slow. I`m thinking there is a...
|
by: silverburgh.meryl |
last post by:
i am trying to use python to walk thru each subdirectory from a top
directory. Here is my script:
savedPagesDirectory = "/home/meryl/saved_pages/data"
dir=open(savedPagesDirectory, 'r')
...
|
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",...
|
by: Martin Marcher |
last post by:
Hello,
I'm playing around with os.walk and I made up del_tree(path) which I
think is correct (in terms of the algorithm, but not as python wants
it :)).
As soon as some directory is deleted...
|
by: Jeff McNeil |
last post by:
Your args are fine, that's just the way os.path.walk works. If you
just need the absolute pathname of a directory when given a relative
path, you can always use os.path.abspath, too.
A couple...
|
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):...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |