Python Forum
sub-folders in folders from text line
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
sub-folders in folders from text line
#1
Hi!
Tips on how to create sub-folders in each one created from an txt dokument. See code example.
Should I also close the text file?

import os

with open('text.txt') as x:            *creates folders named from textdokument*
    for line in x:
        line = line.strip()
        os.mkdir(line)                 *Here I want to create sub-folder in each folder*
Tanks in advance!
// Jens
Reply
#2
I don't understand the question. You can use os.mkdir to create directories. There's also os.makedirs if you want to give a path and create all intermediate directories to the leaf (i.e. like mkdir -p on the command line would do).

Also, no, you don't need to close the file - using with takes care of that for you (go and read about context managers if you're interested).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Prevention of deleting and creating folders kayak88 1 503 Mar-10-2025, 02:19 PM
Last Post: DeaD_EyE
  Python Folders Splitter to Files Splitter Guybrush16bit 12 1,300 Mar-01-2025, 08:17 PM
Last Post: snippsat
  Using zipfile module - finding folders not files darter1010 2 2,047 Apr-06-2024, 07:22 AM
Last Post: Pedroski55
  What script to paste folders thenewcoder 1 1,287 Nov-29-2023, 09:40 AM
Last Post: Pedroski55
  Create new folders and copy files cocobolli 3 4,374 Mar-22-2023, 10:23 AM
Last Post: Gribouillis
  Copy only hidden files and folders with rsync Cannondale 2 2,265 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 2,628 Feb-08-2023, 07:45 PM
Last Post: Larz60+
  python move folders and subfolders not working mg24 5 4,315 Nov-09-2022, 02:24 PM
Last Post: Larz60+
  Graphic line plot with matplotlib, text file in pytho khadija 2 2,390 Aug-15-2022, 12:00 PM
Last Post: khadija
  Importing modules from different folders Tomli 3 2,463 Jun-26-2022, 10:44 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