Folder creator
Managing files and directories can at time be a hassle, but with a little bit of Python, you can automate this process easily. Whether you’re studying, organizing projects, preparing for a big task, or just trying to keep your files tidy, this script will help you create directories and subfolders with minimal effort. The best part? It’s cross-platform, and works on Windows and macOS/Linux.
The Script
Here’s a straightforward Python script that:
- Prompts the user to specify a working directory.
- Checks if the directory exists, and if not, offers to create it.
- Optionally creates a series of subfolders within the working directory.
import os
work_dir = input("Enter the work directory: ")
if not os.path.exists(work_dir):
create_dir = input(f"The directory '{work_dir}' does not exist. Create it? (y/n): ")
if create_dir.lower() == 'y':
os.makedirs(work_dir)
else:
print("Exiting script.")
exit()
create_folders = input("Create subfolders within the work directory? (y/n): ")
if create_folders.lower() == 'y':
folder_name = input("Enter the naming scheme for the subfolders: ")
num_folders = int(input("Enter the number of subfolders to create: "))
for i in range(1, num_folders+1):
folder_path = os.path.join(work_dir, folder_name + str(i))
if not os.path.exists(folder_path):
os.makedirs(folder_path)
else:
print(f"The folder '{folder_path}' already exists.")
else:
print("Exiting script.")
exit()
How It Works
- Prompt for Directory: The script starts by asking the user for the directory path where the work will be done.
- Check and Create Directory: It checks if the directory exists. If it doesn’t, the user can choose to create it.
- Create Subfolders: If the user wants to create subfolders, the script will ask for a naming scheme and the number of subfolders needed, then create them accordingly.
Cross-Platform Compatibility
This script is designed to be cross-platform. As It uses import os, os.path.exists
to check for directory existence and os.makedirs
to create directories, both of which are part of Python’s standard library and work consistently across Windows, macOS, and Linux.
Running the Script
To run this script, you need to have Python installed on your system. Simply copy the script into a .py
file and run it using your terminal or command prompt:
python your_script_name.py
Replace your_script_name.py
with the name of your file. The script will then guide you through the process interactively.
Conclusion
With this script, you can easily set up your project directories and subfolders, making your workflow more efficient. It’s a simple yet powerful tool that saves you time and effort, ensuring your files are organized exactly how you want them. Give it a try and see how it can streamline your file management tasks!
Happy crafting! 🚀