Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Folders in folders
#1
Hi!
Trying to create three different sub-folders named: "raw" "right" and "left" in one folder named from a txt file. I succeeded to make one sub-folder, "raw" in each.

import os
path = '/raw'
with open('regina.txt') as x:
    for line in x:
        line = line.strip()
        os.makedirs(line + path) 
Would appreciate tips on how to think/ solve this. Think
Thanks in advance!
Reply
#2
use a for loop to iterate over folders you want to create. Also use os.path.join to create paths

import os
sub_folders = ('raw', 'right', 'left')
with open('regina.txt') as x:
    for line in x:
        line = line.strip()
        for sub_folder in sub_folders:
            os.makedirs(os.path.join(line, sub_folder))
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
#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
#4
@DeaD_EyE - they want to read the parent folder(s) from text file and then create 3 subfolders - raw, right, left

EDIT: @DeaD_EyE changed their code according to OP question.
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
#5
I edited my post a litte bit.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#6
Great! I will try this - Thanks!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  What script to paste folders thenewcoder 1 639 Nov-29-2023, 09:40 AM
Last Post: Pedroski55
  Create new folders and copy files cocobolli 3 1,334 Mar-22-2023, 10:23 AM
Last Post: Gribouillis
  Copy only hidden files and folders with rsync Cannondale 2 954 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,735 Feb-08-2023, 07:45 PM
Last Post: Larz60+
  python move folders and subfolders not working mg24 5 2,072 Nov-09-2022, 02:24 PM
Last Post: Larz60+
  Importing modules from different folders Tomli 3 1,428 Jun-26-2022, 10:44 AM
Last Post: snippsat
  Code to check folder and sub folders for new file and alert fioranosnake 2 1,874 Jan-06-2022, 05:03 PM
Last Post: deanhystad
  automatic create folders Mr_Kool 4 1,718 Dec-21-2021, 04:38 PM
Last Post: BashBedlam
  Moving files to Folders giddyhead 13 9,005 Mar-07-2021, 02:50 AM
Last Post: giddyhead
  Properly import from sub/parent folders? oclmedyb 2 2,015 Dec-14-2020, 12:22 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

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