469,902 Members | 1,950 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,902 developers. It's quick & easy.

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 27918
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

Post your reply

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

Similar topics

3 posts views Thread by Ya Ya | last post: by
3 posts views Thread by erwinschrijver | last post: by
2 posts views Thread by Kevin | last post: by
2 posts views Thread by Allerdyce.John | last post: by
10 posts views Thread by mistral | last post: by
4 posts views Thread by durumdara | last post: by
1 post views Thread by gowthamkg | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.