472,345 Members | 1,519 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,345 software developers and data experts.

Python code to search a folder(not file) inside another folder

Hi, I am new to python. I have to devlope a small for code for getting the path of a particular folder, that is given as input to the code, in the other folder. Basically i have to search a folder, not file, inside another folder. I work on windows platform. Can anybody help me.
Aug 24 '08 #1
3 29546
boxfish
469 Expert 256MB
Hi,
I'm not sure what you're trying to do. It sounds like some of the functions in the os.path module might be useful for this.
Aug 24 '08 #2
bvdet
2,851 Expert Mod 2GB
The following returns a list of the folders named "dir_name" found under "head_dir". The folder and its subfolders are recursively searched.
Expand|Select|Wrap|Line Numbers
  1. import os
  2.  
  3. def dir_list_folder(head_dir, dir_name):
  4.     """Return a list of the full paths of the subdirectories
  5.     under directory 'head_dir' named 'dir_name'"""
  6.     dirList = []
  7.     for fn in os.listdir(head_dir):
  8.         dirfile = os.path.join(head_dir, fn)
  9.         if os.path.isdir(dirfile):
  10.             if fn.upper() == dir_name.upper():
  11.                 dirList.append(dirfile)
  12.             else:
  13.                 # print "Accessing directory %s" % dirfile
  14.                 dirList += dir_list_folder(dirfile, dir_name)
  15.     return dirList
  16.  
  17. if __name__ == '__main__':
  18.     for item in dir_list_folder(r'D:\SDS2_7.1C', 'mem'):
  19.         print item
Example output:
>>> D:\SDS2_7.1C\jobs\610_CC4_71\mem
D:\SDS2_7.1C\jobs\613_Ironstone_Bank\mem
D:\SDS2_7.1C\jobs\614_Embraer\mem
D:\SDS2_7.1C\jobs\615_Ironstone_OK\mem
D:\SDS2_7.1C\jobs\616_Greenway\mem
D:\SDS2_7.1C\jobs\617_Villa\mem
D:\SDS2_7.1C\jobs\618_Johnston\mem
D:\SDS2_7.1C\jobs\619_Okaloosa_Walton\mem
D:\SDS2_7.1C\jobs\Great_Wolf\mem
>>>

The following does the same thing, but uses os.walk():
Expand|Select|Wrap|Line Numbers
  1. import os
  2.  
  3. def dir_list_folder(head_dir, dir_name):
  4.     outputList = []
  5.     for root, dirs, files in os.walk(head_dir):
  6.         for d in dirs:
  7.             if d.upper() == dir_name.upper():
  8.                 outputList.append(os.path.join(root, d))
  9.     return outputList
  10.  
  11. print '\n'.join(dir_list_folder(r'D:\SDS2_7.1C\jobs', 'mem'))
Aug 24 '08 #3
Expand|Select|Wrap|Line Numbers
  1. def dir_list_folder(head_dir, dir_name):
  2.    dir_list = []
  3.    cmd = "cd " + head_dir + " && find . -type d -name " + dir_name + " > /tmp/temp.txt && cd - "
  4.    (status, output)  =  commands.getstatusoutput(cmd)
  5.    if status != 0:
  6.       print "Error : Failed to execute cmd : " + cmd 
  7.    fp = open("/tmp/temp.txt")
  8.    lines = fp.readlines()
  9.    fp.close()
  10.    for line in lines:
  11.       dir_list.append(line])
  12.    return dir_list
Aug 24 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: mm | last post by:
Can someone with Win 2K please help me here. Best yet, can some MVP duplicate it and report it to MS. I can't find any reference to this issue on...
3
by: Ya Ya | last post by:
I have a folder with a lot of PDF and CHM files. I would like to develope an ASP.net application that enables the user to search inside the content...
3
by: erwinschrijver | last post by:
Is it possible to search inside a PDF-flie, using PHP? Same question for MS Word documents? Searched for it, but I cannot find anything about it,...
2
by: Kevin | last post by:
i have a python file called pyq which outputs stock quotes, currently i also have a html file that takes stock ticker inputs, i would like to...
2
by: Allerdyce.John | last post by:
Is there a python library to parse C++ code file? If yes, can you please tell me where is it? Thank you.
10
by: mistral | last post by:
I have installed ActivePython http://www.activestate.com/Products/ActivePython/ How I can run Python file, test.py?
2
by: yinglcs | last post by:
How to modify the source of a python file inside a python egg file? I can see the file by unzipping it, but how can I package it back as a python...
3
by: yinglcs | last post by:
Hi, i have 2 python files in *different directory* , how can I import python functions from 1 python file to another? i get this error: import...
4
by: durumdara | last post by:
Hi! My problem is that: The "test.py" is working in every machine that I use except in my home machine. Formerly it worked but now not. I don't...
1
by: gowthamkg | last post by:
Hi all, im new to php.i want to write a php program for search inside a website without MYSQL .Im using Windows Xp and installed Wamp...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...

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.