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
  Using zipfile module - finding folders not files darter1010 2 246 Apr-06-2024, 07:22 AM
Last Post: Pedroski55
  What script to paste folders thenewcoder 1 664 Nov-29-2023, 09:40 AM
Last Post: Pedroski55
  Create new folders and copy files cocobolli 3 1,440 Mar-22-2023, 10:23 AM
Last Post: Gribouillis
  Copy only hidden files and folders with rsync Cannondale 2 997 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,790 Feb-08-2023, 07:45 PM
Last Post: Larz60+
  python move folders and subfolders not working mg24 5 2,152 Nov-09-2022, 02:24 PM
Last Post: Larz60+
  Graphic line plot with matplotlib, text file in pytho khadija 2 1,375 Aug-15-2022, 12:00 PM
Last Post: khadija
  Importing modules from different folders Tomli 3 1,456 Jun-26-2022, 10:44 AM
Last Post: snippsat
  Skipping line in text without Restarting Loop IdMineThat 4 1,475 Apr-05-2022, 04:23 AM
Last Post: deanhystad
  Find and delete above a certain line in text file cubangt 12 3,453 Mar-18-2022, 07:49 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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