1

I would like to find the path to specific folders that have only specific folders brothers

ex: I want to find all folders named : zeFolder with siblings folders brotherOne and brotherTwo

|-dad1
|---brotherOne
|---brotherFour
|---zeFolder (not match)

|-dad2
|---brotherOne
|---brotherTwo
|---zeFolder (♥♥♥Match♥♥♥)

[...]

Below is my code, but with this solution I find all the folders.

import os
for root, dirs, files in os.walk("/"):
    #print (dirs)
    for name in dirs:
        if name == 'totolo':
                print ('finded')
                print(os.path.join(root, name))

I don't know how to use Conditional Statements to do that

Thanks for you help.

jmercier
  • 564
  • 2
  • 7
  • 17
  • Did you want `brotherOne` and `brotherTwo` to be the only siblings or can there be other siblings aswell and it would match as long as those two were there? – Paul Rooney Mar 02 '15 at 23:36

3 Answers3

2

Basically it sounds like you want to find a specific set of subfolders so using sets is both natural and makes this a fairly easy thing to do. Their use also removes order dependencies when checking for equality.

import os

start_path = '/'
target = 'zeFolder'
siblings = ['brotherOne', 'brotherTwo']
sought = set([target] + siblings)

for root, dirs, files in os.walk(start_path):
    if sought == set(dirs):
        print('found')
martineau
  • 119,623
  • 25
  • 170
  • 301
1

What about using lists

import os

folder = 'zeFolder'
brothers = ['brotherOne', 'brotherTwo']

for dirpath, dirnames, filenames in os.walk('/'):
    if folder in dirnames and all(brother in dirnames for brother in brothers):
        print 'matches on %s' % os.path.join(dirpath, 'zeFolder') 

or sets

import os

folder = 'zeFolder'
brothers = set(['brotherOne', 'brotherTwo', folder])

for dirpath, dirnames, filenames in os.walk('/'):
    if set(dirnames).issuperset(brothers) :
        print 'matches on %s' % os.path.join(dirpath, 'zeFolder') 

Both run at same speed for me.

Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
  • hi, thanks for your awser, I must use latin char and when I print a folder like "é à" etc, I have an error, so I use : .encode('utf-8') but now i have a b' at the start of my string (bytes) how can i remove it ? and keep utf-8 ty – jmercier Mar 03 '15 at 09:52
  • I'll try to answer you tomorrow if someone hasn't already. I don't know if this helps http://stackoverflow.com/questions/28246004/cant-get-a-degree-symbol-into-raw-input/28246283#28246283 ? – Paul Rooney Mar 03 '15 at 10:14
  • Try decode rather than encode. http://stackoverflow.com/questions/5974585/python-not-able-to-open-file-with-non-english-characters-in-path – Paul Rooney Mar 03 '15 at 10:25
  • when i tried to decode, traceback : 'str object has no attrbute decode' – jmercier Mar 03 '15 at 10:37
  • I cheat with this snippet of code myData = myData[2:-1], i know it's dirt; if you have time, ty for your awnser – jmercier Mar 03 '15 at 10:49
  • in fact it does not solve anything lol, when i try to copy with "shutil", the path is a byte :' ( and i always have an when i try to decode 'str object has no attrbute decode' – jmercier Mar 03 '15 at 11:21
0
import os
import glob

filelist = glob.glob(r"dad1/*brotherOne")
for f in filelist:
    print(f)

filelist = glob.glob(r"dad1/*brotherTwo")
for f in filelist:
    print(f)

You could also try the glob technique. And do whatever action you'd like to in the for loop.

Death_Dealer
  • 325
  • 4
  • 17