π Automatically creates a new folder after a new folder is named.
- A new folder is created without a name.
- The new folder is automatically named.
- Another new folder is created without a name.
- The second new folder is automatically named.
- Repeat step 1.
- Repeat step 2.
- Repeat step 3.
import os
def create_new_folder(base_dir):
# Get a list of all folders in the base directory
folders = [folder for folder in os.listdir(base_dir) if os.path.isdir(os.path.join(base_dir, folder)]
# Find the highest numbered folder
max_num = 0
for folder in folders:
try:
folder_num = int(folder)
if folder_num > max_num:
max_num = folder_num
except ValueError:
continue
# Create a new folder with the next number
new_folder_name = str(max_num + 1)
new_folder_path = os.path.join(base_dir, new_folder_name)
os.mkdir(new_folder_path)
return new_folder_name
if __name__ == "__main__":
base_directory = "./" # Change this to the directory where you want to create the folders
# Create the first folder
create_new_folder(base_directory)
print("Created the first folder.")
# Create the second folder
create_new_folder(base_directory)
print("Created the second folder.")
Make sure to change the base_directory variable to the directory where you want to create the folders. This script will automatically name new folders with incremental numbers (e.g., 1, 2, 3, etc.) in the specified directory.