I want to use os.makedirs to create a directory structure. I tried using braceexpand, and it worked in part:
for x in list(braceexpand('/Users/admin/Desktop/{cat,dog,bird}/{thing1,thing2,thing3,thing4}')): os.makedirs(x,exist_ok=True)
But instead of manually creating directories named 'cat', 'dog' and 'bird', I want to automate the process by creating the structure from a list.
animals = [ 'cat', 'dog', 'bird']
This is because I sometimes want only the intermediate directory 'dog', and sometimes 'dog' and 'cat', and sometimes all three. It would therefore be nice to be able to create the directory structure based on whatever subset of animals the list contains.
I haven't been able to implement this (braceexpand'ing using a list) so far.
With help from the answers here (Brace expansion in python glob), I was able to create the directory structure I wanted using nested for-loops with the code below.
for x in ['/Users/admin/Desktop/{s}/{b}'.format(b=b,s=s) for b in ['thing1','thing2','thing3','thing4'] for s in ['cat', 'dog', 'bird']]: os.makedirs(x,exist_ok=True)
But I like the readability of braceexpand, so I was curious whether anyone knew how to do it with that – or whether it is something that can even be done.
Thanks!
You should be able to "hack" it using Python string interpolation before passing the string to braceexpand
:
names = ['dog', 'cat', 'bird']
print('/Users/admin/Desktop/{{{}}}/{{thing1,thing2,thing3,thing4}}'.format(','.join(names)))
Outputs
/Users/admin/Desktop/{dog,cat,bird}/{thing1,thing2,thing3,thing4}
Of course the same can be done for thing1,thing2,...
For an explanation why {{{}}}
and {{ ... }}
is needed, see this answer.