By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,676 Members | 2,262 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,676 IT Pros & Developers. It's quick & easy.

help needed using os.path.walk [solved]

P: 3
hello friends,

i am using the os.path.walk to search to filter the files with the given word, this function i m using in many modules. i just want to place this function in some class and call it whenever required by passing the necessary word. for e.g i want to filter files with "Spec" which is specification documents and store in some list, and other time with word "doc" like this many i want to place it in some class placed in some file and access it by calling the function with the filtering word required, like spec, doc etc.... can any one say me how to pass this filtering word to the calling function from os.path.walk...

here is some sample code which i use to filter the documents with spec:

def finddocfiles(option,dir,files):
for index in files:
#print index
if index.rfind("Spec")!=-1:

# all spec files are stored in list_Sharedfiles list

this is called by os.path.walk(path,finddocfiles,list_files)

so i use this very frequently to filter files, so any one please suggest me how to pass the filtering word i.e here "Spec" or "doc" waiting for ur reply...thans in advance
Oct 16 '06 #1
Share this Question
Share on Google+
1 Reply

Expert 5K+
P: 6,596
walk( path, visit, arg)
Calls the function visit with arguments (arg, dirname, names)

arg can be anything including a tuple, right.
so use

Expand|Select|Wrap|Line Numbers
  1. def finddocfiles(options, dir, files): 
  2.     for index in files:
  3.         #print index
  4.         if index.rfind(options[1])!=-1:
  6.         # all spec files are stored in list_Sharedfiles list
  7.         options[0].append(dir+os.sep+index)
  10. key = "Spec"
  11. os.path.walk(path, finddocfiles, (list_files, key))
Oct 16 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.