Python Forum
Python create directories within directories
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python create directories within directories
#1
I need to create a directory, then create 24 more directories within the main directory, then add 3 more numbered directories within the 24. This is what I have so far:
import os
def main():
    Directory = 'Notes'
    os.mkdir(Directory)
    Week = 'Week'
    os.makedirs()
    Day = ('Day 1', 'Day 2', 'Day 3')
As you can see I cant figure out how to create the 24 'Week' directories within the main directory and number them.
Wall
Reply
#2
Use pathlib, make it easy on yourself:
from pathlib import Path
import os

# Make sure starting point is script directory
os.chdir(os.path.abspath(os.path.dirname(__file__)))

def create_dir_tree():
    homepath = Path('.')
    
    # put all in a container directory, I'll call it AllWeeks
    apath = homepath / 'AllWeeks'
    apath.mkdir(exist_ok=True)

    # build the rest
    for weekno in range(1, 25):
        wkpath = apath / f"week{weekno}"
        wkpath.mkdir(exist_ok=True)
        for dayno in range(1,8):
            daypath = wkpath / f"day{dayno}"
            daypath.mkdir(exist_ok=True)

if __name__ == '__main__':
    create_dir_tree()
Reply
#3
awesome! thank you, i'll give a go!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Organization of project directories wotoko 3 423 Mar-02-2024, 03:34 PM
Last Post: Larz60+
  Navigating file directories and paths inside Jupyter Notebook Mark17 5 687 Oct-29-2023, 12:40 PM
Last Post: Mark17
  Listing directories (as a text file) kiwi99 1 834 Feb-17-2023, 12:58 PM
Last Post: Larz60+
  Find duplicate files in multiple directories Pavel_47 9 3,074 Dec-27-2022, 04:47 PM
Last Post: deanhystad
  rename same file names in different directories elnk 0 711 Nov-04-2022, 05:23 PM
Last Post: elnk
  I need to copy all the directories that do not match the pattern tester_V 7 2,408 Feb-04-2022, 06:26 PM
Last Post: tester_V
  Functions to consider for file renaming and moving around directories cubangt 2 1,746 Jan-07-2022, 02:16 PM
Last Post: cubangt
  Make nested system directories based on an unsorted list? koebi 0 1,595 Mar-25-2020, 01:14 PM
Last Post: koebi
  Shutil attempts to copy directories that don't exist ConsoleGeek 5 4,529 Oct-29-2019, 09:26 PM
Last Post: Gribouillis
  How to combine file names into a list from multiple directories? python_newbie09 3 5,207 Jul-09-2019, 07:38 PM
Last Post: python_newbie09

Forum Jump:

User Panel Messages

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