I'm not certain I understand what you're trying to do, but here is a version of your code that is cleaned up a bit. It assumes the C:\Temp
directory exists, and will create 3 folders in C:\Temp
, and 1 subfolder in each of those 3 folders.
import os
numbers = [1,2,3]
base_path = os.path.join('C:/', 'Temp')
for number in numbers:
# create the directory C:\Temp\{name}
os.mkdir(os.path.join(base_path, f'GD_{number}'))
# create the directory C:\Temp\{name}\{subfolder_name}
os.mkdir(os.path.join(base_path, f'GD_{number}', f'S1_{number}'))
Some Notes and Tips:
- Indentation is part of the syntax in python, so make sure you indent every line that is in a code block (such as your for loop)
- There are many ways to format strings, I like f-strings (a.k.a. string interpolation) which were introduced in python 3.6. If you're using an earlier version of python, either update, or use a different string formatting method. Whatever you choose, be consistent.
- It is a good idea to use
os.path.join()
when working with paths, as you were trying to do. I expanded the use of this method in the code above.
- As another answer pointed out, you can simply iterate over your numbers collection instead of using
range()
and indexing.