Brett Scott
Brett Scott

Reputation: 59

Make multiple directories based on a list

Hi I would like to make multiple new dir's in a set root dir each one named based on a list of names

e.g.

List looks like this

Folder_1

Folder_x

Folder_y

is there an easy way to do this in python?

Upvotes: 5

Views: 35321

Answers (9)

martineau
martineau

Reputation: 123473

Here's one way to do it using a flexible custom function. Note that it uses os.makedirs() instead of os.mkdir() which means that it will also create the root folder if necessary, as well as allowing the subfolder paths to contain intermediate-level directories if desired.

The code also uses functools.partial() to create a temporary local function named concat_path() to use with the built-in map() function to concatenate the root directory's name with each subfolder's. It then uses os.makedirs() on each of those to create the subfolder path.

import os
from functools import partial

def makefolders(root_dir, subfolders):
    concat_path = partial(os.path.join, root_dir)
    for subfolder in map(concat_path, subfolders):
        os.makedirs(subfolder, exist_ok=True)  # Python 3.2+

if __name__=='__main__':
    root_dir = '/path/to/root/folder'
    subfolders = ('Numbers/Folder_1', 'Letters/Folder_x', 'Letters/Folder_y')
    makefolders(root_dir, subfolders)

Upvotes: 3

tony75
tony75

Reputation: 5

import os

dir_names = ["ABC1", "ABC2", "ABC3"]
#Create three folders on Desktop
#dir_path = os.path.expanduser("~/Desktop")
dir_path = os.path.join(os.path.join(os.environ['USERPROFILE']), 'Desktop')

for folder in dir_names:
    try:
        if not os.mkdir(os.path.join(dir_path,folder)):
            print(folder)

    except:

            print("Folder already exists")
            break

Upvotes: 0

d_k
d_k

Reputation: 25

import os

root_dir = 'root_path\\whateverYouWant\\'
list_ = ['Folder_1', 'Folder_x', 'Folder_y']
for folder in list_:
    os.makedirs(root_dir + folder)

Upvotes: 1

Dinesh P. R
Dinesh P. R

Reputation: 51

I was in the same situation too and finally got a small working output, try it.

I had two files, first the program file and second a .txt file containing the list of folder names.

import os
f = open('folder.txt', 'r')
for g in f:
    os.mkdir(g)
f.close()

Upvotes: 0

Muneer Ahmad
Muneer Ahmad

Reputation: 323

from os import makedirs
makedirs('1/2/3/4/5/6/7/8/4/4/5/5/5/5/5/5/5/55/5/5/5/5')

By this, you'll more than you want.

Upvotes: -2

Sagnik
Sagnik

Reputation: 71

Make folder name as desired

import os

root_path = '/home/sagnik'

folders= [None] * 201
for x in range(0,201):
    print(str(x))
    folders[x] ="folder"+str(x)

Create folders

for folder in folders:
    os.mkdir(os.path.join(root_path,folder))

Upvotes: 2

Anake
Anake

Reputation: 7649

import os

root_path = '/whatever/your/root/path/is/'
folders = ['Folder_1','Folder_x','Folder_y']
for folder in folders:
    os.mkdir(os.path.join(root_path,folder))

Upvotes: 18

Yann Vernier
Yann Vernier

Reputation: 15887

os.path.join to combine your root dir and name, and os.mkdir to create the directories. Looping over things is easily enough done with for.

Upvotes: 1

user2665694
user2665694

Reputation:

os.mkdir(name_of_dir) 

is your friend.

Upvotes: 1

Related Questions