I would like to create empty directory folders in a file path based on values in the list

514 Views Asked by At

I have my list

listnumbers='1.0,5.0,6.0,7.0,9.0,10.0'

I have my filepath

 filepath=r'C:\ArcGIS\Projects\Name'

I also have a for loop that attempts to take the values from listnumbers

for line in listnumbers:
  for value in line.split(','):
    os.makedirs(os.path.join(path,value))

though it creates the first empty folder which is 1, it does not create the following folder which would be 5.

Instead i get an error message that says

FileExistsError: [WinError 183] Cannot create a file when that file  already exists : 'C:\ArcGIS\Projects\Name'

I need help. I feel I'am close and might need to make a small adjustment.

1

There are 1 best solutions below

0
B4dWo1f On BEST ANSWER

your code is a bit weird... your listnumbers is not a list but a string, so when you do for line in listnumbers: the variable line runs over each character of listnumbers

This code works for me:

import os

listnumbers = '1.0,5.0,6.0,7.0,9.0,10.0'
folder_root = '/path/to/your/folder' # in your case: C:\ArcGIS\Projects\Name

folders = listnumbers.split(',')

for f in folders:
   os.makedirs(os.path.join(folder_root, f))