Python Forum
create folders named from list array
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
create folders named from list array
#1
Hi,

how can i create a bunch of folders in my specified location and create these folders based on a list of folder names to be created

example my cwd is:

c:\xampp\htdocs\site\

my list of folders to be created inside the cwd is:

  folderstobecreatedlist = (['folder1', 'folder2', 'folder3', 'etc'])  
so my desired outcome would be example:

c:\xampp\htdocs\site\folder1
c:\xampp\htdocs\site\folder2
c:\xampp\htdocs\site\folder3
c:\xampp\htdocs\site\etc

i have tried:

import os
currentpath = "c:\xampp\htdocs\site\"
listOfFoldersToCreate = ['folder1', 'folder2', 'folder3', 'etc']
os.mkdir(currentpath, mode=777
os.mkdir(currentpath, mode)
os.mkdir(currentpath, mode, listOfFoldersToCreate
for foldersToCreate in listOfFoldersToCreate:
    os.mkdir(path, mode)
but as one can tell i have no experience in python and would really like some help please. preferably in a for loop to just iterate over my list of folders to create and make it done
Reply
#2
you need to loop over elements in the list, for each folder name from the list construct the folder path and then use os.mkdir to create the folder.
Also please, don't put every separate line in python tags. Show us the full code as one piece.

import os

folders = ['folder1', 'folder2', 'folder3', 'etc']
base_path = 'c:/xampp/htdocs/site/' # use raw string if you would use backslash e.g. r'c:\xampp\htdocs\site\'
for folder in folders:
    os.mkdir(os.path.join(base_path, fodler))

or if you prefer OOP approach
from pathlib import Path

folders = ['folder1', 'folder2', 'folder3', 'etc']
base_path = 'c:/xampp/htdocs/site/' # use raw string if you would use backslash e.g. r'c:\xampp\htdocs\site\'
for folder in folders:
    Path(base_path, folder).mkdir()
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Delete strings from a list to create a new only number list Dvdscot 8 1,533 May-01-2023, 09:06 PM
Last Post: deanhystad
  Create new folders and copy files cocobolli 3 1,454 Mar-22-2023, 10:23 AM
Last Post: Gribouillis
  How do I add comments from a text-file to an array of folders? clausneergaard 2 1,792 Feb-08-2023, 07:45 PM
Last Post: Larz60+
  [split] why can't i create a list of numbers (ints) with random.randrange() astral_travel 7 1,517 Oct-23-2022, 11:13 PM
Last Post: Pedroski55
  functional LEDs in an array or list? // RPi user Doczu 5 1,601 Aug-23-2022, 05:37 PM
Last Post: Yoriz
  Create array of values from 2 variables paulo79 1 1,090 Apr-19-2022, 08:28 PM
Last Post: deanhystad
  How to store the resulting Doc objects into a list named A xinyulon 1 1,900 Mar-08-2022, 11:49 PM
Last Post: bowlofred
  how to easily create a list of already existing item CompleteNewb 15 3,534 Jan-06-2022, 12:48 AM
Last Post: CompleteNewb
  automatic create folders Mr_Kool 4 1,765 Dec-21-2021, 04:38 PM
Last Post: BashBedlam
  Create SQLite columns from a list or tuple? snakes 6 8,698 May-04-2021, 12:06 PM
Last Post: snakes

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020