Reputation: 67
I have a script that creates a sequence of pictures each time is ran. What I want to do is to create a new folder each time the script runs, so each sequence of pictures is stored in the new folder. Can somebody tell me how to do it? Thanks
Upvotes: 0
Views: 1425
Reputation: 1077
If you want to name the folders with a numerical sequence rather than a timestamp, you can do it like this:
import os
folder_names = filter(os.path.isdir, os.listdir(os.getcwd()))
last_number = max([int(name) for name in folder_names if name.isnumeric()])
new_name = str(last_number + 1).zpad(4)
os.mkdir(new_name)
This finds the largest number that already exists as a folder, then adds one to that number and zero pads it to 4 characters long, and creates a new folder with that name.
Upvotes: 1
Reputation: 1204
Just create a new folder and give it a new name every time your script is run. You could append your folder name with a DateTime stamp, or any other logic that suits your needs.
script.py
import os
import time
def create_images(folder_name):
# Create and store your images in folder_name
pass
if __name__ = "__main__":
folder_name = 'my_folder_' + time.strftime("%Y_%m_%d_%H_%M_%S")
os.mkdir(folder_name)
create_images(folder_name)
Upvotes: 1