Creating / creating directories in python (complex)

I am trying to create a bunch of directories / subdirectories into which I can copy files. I am working with Python and I cannot find a good way to do this. I have a main path from which I will retreat. Then after that I have weights and No_Weights. Men and women are as follows. In each of the male and female folders I have every ethnicity (Caucasian, African American, Asian, Latin American, Indo, other, unknown). In each of these folders, I have an age of 20 to 20+ (B20, 20, 30, 40, 50, 60, 70).

I tried to generate all the paths, so all I would need to call is mkdir about 50 times, but that’s about 150 lines of code (almost).

Is there any easy way to create all these folders without having to do it manually?

+3
source share
5 answers
import itertools
import os

dirs = [["Weights", "No_Weights"],
        ["Male", "Female"],
        ["Caucasian", "African-American", "Asian", "Hispanic", "Indo", "Other", "Unknown"], 
        ["B20", "20", "30", "40", "50", "60", "70"]]

for item in itertools.product(*dirs):
    os.makedirs(os.path.join(*item))

itertools.product()builds all possible variations of the path, then os.path.join()merges the subfolders together using the correct syntax for your platform.

EDIT: os.makedirs()required instead os.mkdir(). Only the first will build all the intermediate subdirectories in the full path.

+17
source

In this example, you need to start:

import itertools
import os.path

ROOT = r'C:\base\path'

sex = ('male', 'female')
ethnicity = ('Caucasian', 'African-American', 'Asian')
ages = ('B20', '20', '30')

for path in itertools.product(sex, ethnicity, ages):
    print os.path.join(ROOT, *path)

The itertools module is your friend: http://docs.python.org/library/itertools.html#itertools.product

+3
source

- :

main = 'somedir'
weight = ['weights', 'No_weights']
ethnicity = ['Caucasian', #the rest]
ages = ['B20'] +  range(20, 71, 10)

for w in weights:
    os.mkdir(os.path.join(main, w)
    for e in ethnicity:
        os.mkdir(os.path.join(main, w, e))
        for a in ages:
            os.mkdir(os.path.join(main, w, e, a))

...

+2

for-loops, os.mkdir . os.path.join, .

- :

loop weights
    mkdir weight
    loop sexes
        mkdir weights + sex
        loop ethnicities
            mkdir weights + sex + ethnicity
            loop ages
                mkdir weights + sex + ethnicity + age

:

for weight in ('weights', 'no_weights'):

mkdir - os.mkdir

'+' - os.path.join:

os.mkdir(os.path.join(weights, sex, ethnicity, age))

: dir_util , :

http://docs.python.org/release/2.5.2/dist/module-distutils.dirutil.html

loop weights
    loop sexes
        loop ethnicities
            loop ages
                mkpath weights + sex + ethnicity + age
0

os.makedirs - "".

( " A, B,..." ) " " - , s/thing like...

choices = [ ['Weights', 'Noweights'],
            ['Male', 'Female'],
            ['Caucasian', 'AfricanAmerican', ...
            ...
          ]
Ls = [len(x) for x in choices]
ct = [0] * len(Ls)

while True:
    p = [c[i] for i, c in zip(ct, choices)]
    os.makedirs(os.path.join(p))
    n = -1
    while n > -len(Ls):
      ct[n] += 1
      if ct[n] < Ls[n]: break
      ct[n] = 0
      n -= 1
    else:
      break

itertools.product - " A .. .." - :

for p in itertools.product(*choices):
    os.makedirs(os.path.join(p))

can replace all the above code (!). I think it's also worthwhile to be aware of the “mixed-base-counting” approach with a low abstraction level, because it is useful in many cases (including the time during which it gets stuck using the Python release ;-) < 2.6.

0
source

Source: https://habr.com/ru/post/1752653/


All Articles