If I have a folder test which has multiple subfolders A, B, C, etc., each has same structure sub1, sub2 and sub3:
├─A
│ ├─sub1
│ ├─sub2
│ └─sub3
├─B
│ ├─sub1
│ ├─sub2
│ └─sub3
└─C
| ├─sub1
| ├─sub2
| └─sub3
...
I want to create subfolders named a and b only in sub1 and sub2 and ignore other subfolders (sub3...). This is expected result:
├─A
│ ├─sub1|--a
│ | |--b
│ ├─sub2|--a
│ | |--b
│ └─sub3
├─B
│ ├─sub1|--a
│ | |--b
│ ├─sub2|--a
│ | |--b
│ └─sub3
└─C
│ ├─sub1|--a
│ | |--b
│ ├─sub2|--a
│ | |--b
└─sub3
...
I can create folders a and b with code below, but I don't how can I do this sub1 and sub2? Thanks.
import os
root_path =r"D:\test"
sub_folders = ['a', 'b']
folders = []
for path in os.listdir(root_path):
folders.append(os.path.join(root_path, path))
for f in folders:
os.chdir(f)
for sub_folder in sub_folders:
os.mkdir(sub_folder)
Update: the code below doesn't create subfolders a and b:
sub_folders = ['a', 'b']
folders = []
for path in os.listdir(root_path):
if path in ('sub1', 'sub2'):
folders.append(os.path.join(root_path, path))
for f in folders:
os.chdir(f)
for sub_folder in sub_folders:
os.mkdir(sub_folder)
UPDATE: the code below works, thanks to @Baltschun Ali.
import os
path = 'C:/Users/User/Desktop/test1'
def create_sub_folder (*arg):
if len(arg) < 3:
for i in arg[0]:
for j in arg[1]:
path = i+"/"+j
if os.path.exists(path) is False:
os.makedirs(path)
else:
arg1 = [i+"/"+j for i in arg[0] for j in arg[1]]
create_sub_folder(arg1,*(i for i in arg[2:]))
for dir in os.listdir(path):
if os.path.isdir(os.path.join(path, dir)):
print(dir)
create_sub_folder([dir], ['sub1', 'sub2'],['a','b'])

