Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Folders in folders
#3
from pathlib import Path


path = Path('/raw')
with open('regina.txt') as fd:
    for subdir in fd:
        subdir = subdir.strip()
        (path / subdir).mkdir(parents=True, exist_ok=True)
This creates the Paths /raw/{line_in_regina.txt} ...
This creates also the parent path raw. If a directory already exists (exist_ok=True), then it's continuing without any error.
The os.path is old style, pathlib has many convenient methods.

If the names of the folders should be in ('raw', 'right', 'left'), then the code is a bit different.

from pathlib import Path

pasths = ('raw', 'right', 'left')
with open('regina.txt') as fd:
    for path in paths:
        for subdir in fd:
            subdir = subdir.strip()
            Path(path, subdir).mkdir(parents=True, exist_ok=True)
This will create the directories ('raw', 'right', 'left') and will create in each directory the subdirectories from regina.txt.
Is that what you want?
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
Folders in folders - by jenost - Apr-26-2020, 10:44 AM
RE: Folders in folders - by buran - Apr-26-2020, 10:48 AM
RE: Folders in folders - by DeaD_EyE - Apr-26-2020, 11:41 AM
RE: Folders in folders - by buran - Apr-26-2020, 11:41 AM
RE: Folders in folders - by DeaD_EyE - Apr-26-2020, 11:47 AM
RE: Folders in folders - by jenost - Apr-26-2020, 05:35 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Using zipfile module - finding folders not files darter1010 2 330 Apr-06-2024, 07:22 AM
Last Post: Pedroski55
  What script to paste folders thenewcoder 1 695 Nov-29-2023, 09:40 AM
Last Post: Pedroski55
  Create new folders and copy files cocobolli 3 1,536 Mar-22-2023, 10:23 AM
Last Post: Gribouillis
  Copy only hidden files and folders with rsync Cannondale 2 1,071 Mar-04-2023, 02:48 PM
Last Post: Cannondale
  How do I add comments from a text-file to an array of folders? clausneergaard 2 1,840 Feb-08-2023, 07:45 PM
Last Post: Larz60+
  python move folders and subfolders not working mg24 5 2,289 Nov-09-2022, 02:24 PM
Last Post: Larz60+
  Importing modules from different folders Tomli 3 1,503 Jun-26-2022, 10:44 AM
Last Post: snippsat
  Code to check folder and sub folders for new file and alert fioranosnake 2 1,991 Jan-06-2022, 05:03 PM
Last Post: deanhystad
  automatic create folders Mr_Kool 4 1,825 Dec-21-2021, 04:38 PM
Last Post: BashBedlam
  Moving files to Folders giddyhead 13 9,293 Mar-07-2021, 02:50 AM
Last Post: giddyhead

Forum Jump:

User Panel Messages

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