473,756 Members | 3,482 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

1 New Member
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 30171
boxfish
469 Recognized Expert Contributor
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 Recognized Expert Moderator Specialist
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\jo bs\610_CC4_71\m em
D:\SDS2_7.1C\jo bs\613_Ironston e_Bank\mem
D:\SDS2_7.1C\jo bs\614_Embraer\ mem
D:\SDS2_7.1C\jo bs\615_Ironston e_OK\mem
D:\SDS2_7.1C\jo bs\616_Greenway \mem
D:\SDS2_7.1C\jo bs\617_Villa\me m
D:\SDS2_7.1C\jo bs\618_Johnston \mem
D:\SDS2_7.1C\jo bs\619_Okaloosa _Walton\mem
D:\SDS2_7.1C\jo bs\Great_Wolf\m em
>>>

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
jarfoss
1 New Member
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
5790
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 the Web, or in MS Knowledge Base. I have been able to duplicate this problem on two different Win 2K installations. Steps: 1. Win 2K installation, SP4 with .NET 1.1 installed
3
6545
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 of those files. How do I search inside those type of files ? Thanks for your time ra294@hotmail.com
3
15021
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, there are also no PEAR-packages available... anyone?
2
2813
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 bridge the two by building another program that takes the html inputs and uses them to call the pyq stock ticker program and then output them into a text file... any idea how to do this? my tentative code is:
2
1679
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
2356
by: mistral | last post by:
I have installed ActivePython http://www.activestate.com/Products/ActivePython/ How I can run Python file, test.py?
2
9897
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 egg file after my modification. Thank you.
3
12090
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 task ImportError: No module named task/ Thank you.
4
4599
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 know what happened, I searching for some solution. The errormessage is this (after I tried to start test.py under cmd.exe):
1
1711
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 server.Please help me.
0
10046
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9886
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9722
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7259
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6542
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5155
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5318
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3817
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2677
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.