Skip to content

Create folders from a text file, line by line with Python

Create folders from a text file, line by line with Python - Do you sometimes 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 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 :

Folder1
Folder2
Folder3

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()
root.withdraw()

# Show the first info box
messagebox.showinfo(
"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:
quit()

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

if not dirname:
quit()

# Error handling variables
file_error_list = []
error_occurred = False

try:
# 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
try:
# Create the new directory
os.mkdir(full_path)
except (FileNotFoundError, IOError):
file_error_list.append(filename)
error_occurred = True

except (FileNotFoundError, IOError):
messagebox.showwarning(
"Something went wrong, exiting",
"Tried to create folders here: {}".format(dirname)
)

if not error_occurred:
messagebox.showinfo(
"Folders created",
"You can find your folders here: {}".format(dirname)
)

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

Happy coding!