Create folders from a text file, line by line with Python - Do you sometimes have the need to create a lot of folders from a naming scheme? First, you navigate to your parent folder and right-click to create a new folder. You copy paste the given name and then move on to the next one. This is fine if you're creating a couple of folders, but what about creating 20 or even 100 folders at one time? Read on to discover how to create folders effortlessly from a text file.

We start by creating a text file where we have our folder names. Input a couple of folder names on each line, for example :


Save the file and start up your favorite IDE for some coding. We will create a program that first asks for the folder name file with a filedialog. See an earlier post for more information about filedialogs in Tkinter. The program will then ask where to create the file. We will also put in some small error handling routines.

import os
import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox

This program creates folders by reading input from a text file.
Each line in the file creates one folder.

# Create the Tkinter app
root = tk.Tk()

# Show the first info box
            "Folder creation tool",
            "Welcome, please provide a file containing the folder names and an output folder." 

# Get the file and folder path
open_file_path = filedialog.askopenfilename(parent=root,initialdir="/",title='Please select a file to read the folder names from')

if not open_file_path:

dirname = filedialog.askdirectory(parent=root,initialdir="/",title='Please select a directory where the folders are created')

if not dirname:
# Error handling variables    
file_error_list = []
error_occurred = False

    # Open file
    with open(open_file_path, 'rU') as x:
        # Loop through each line
        for line in x:
            # Remove possible whitespace characters in beginning and end with strip()
            # split the string into a list with split()
            line = line.strip().split()
            # Concatenate the list items
            filename = " ".join([i for i in line])
            # create the full directory path
            full_path = dirname + "/" + filename
                # Create the new directory
            except (FileNotFoundError, IOError):
                error_occurred = True
except (FileNotFoundError, IOError):
            "Something went wrong, exiting",
            "Tried to create folders here: {}".format(dirname)     
if not error_occurred:    
        "Folders created",
        "You can find your folders here: {}".format(dirname) 

    file_error_string = ""
    for file_name in file_error_list:
        file_error_string += file_name
        file_error_string += "\n"   
        "Error creating folders",
        "Tried to create folders here: {}".format(dirname) + "\n"\
        "Following folders could not be created:\n" +

Happy coding, and have a nice Midsummer!

linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram