0

I made a folder and inside there are 100 subfolders which are made by parameters. Now I want to create one subfolder inside each of this 100 subfolders. But whatever I am doing it is not working. I added a simple example.

number=[1,2,3]

for i in range (len(number)):

 Name = 'GD_%d'%(number[i])
 os.mkdir('C:/Temp/t2_t1_18/'+Name)    #till this works fine

subfolder_name='S1_%d'%(number[i])

#This does not work and idea somehow not correct
  os.mkdir(os.path.join('C:/Temp/t2_t1_18/Name'+subfolder_name))
martineau
  • 119,623
  • 25
  • 170
  • 301
  • ```os.mkdir(os.path.join('C:/Temp/t2_t1_18/Name/'+subfolder_name))``` You forgot to add a ```/```. Now, it is making a folder in ```Name``` parent folder. Also, when you are already doing ```os.path.join()```, why do you need string concatenation? ```os.mkdir(os.path.join('C:/Temp/t2_t1_18/Name',subfolder_name))``` –  Aug 18 '21 at 15:31
  • Generally speaking, you usually don't need to use string concatenation (`+`) — i.e. explicitly adding a `'/'` — when using `os.path.join()`. All that's need here is `os.mkdir(os.path.join('C:/Temp/t2_t1_18, 'Name', subfolder_name))` because it accepts more than two arguments. See the [documentation](https://docs.python.org/3/library/os.path.html#os.path.join). – martineau Aug 18 '21 at 16:26

3 Answers3

0

Some Notes

  1. It is better not to use string concatenation when concatenating paths.
  2. Since you just need the numbers it is better to iterate over them, instead of using range
  3. You can take a look at python's new way of formatting https://realpython.com/python-f-strings/

Assuming I got your question right and you want to create a subdirectory in the newly created directory, I would do something like that

import os

numbers = [1,2,3]
main_dir = os.path.normpath('C:/Temp/t2_t1_18/')

for number in numbers:
    dir_name = f'GD_{number}'
    # dir_name = 'GD_{}'.format(number) # python < 3.6
    dir_path = os.path.join(main_dir, dir_name)
    os.mkdir(dir_path)

    subdir_name = f'S1_{number}'
    subdir_path = os.path.join(dir_path, subdir_name)
    os.mkdir(subdir_path)

tchar
  • 838
  • 9
  • 12
0

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.
0

There is a better answer to your question already.

In your example this should be an easy solution (if your Python version is sufficient):

from pathlib import Path


numbers = (1, 2, 3, 4)

for n in numbers:
    Path(f"C:/Temp/t2_t1_18/GD_{n}/S1_{n}").mkdir(parents=True, exist_ok=True)
SvenTUM
  • 784
  • 1
  • 6
  • 16