Directory generation is os.mkdir (path) Take a screenshot at pyautogui.screenshot () Screenshots are saved to save ("save to")
All chords
import os
import pyautogui
import time
import datetime
#Save destination path
h_path = "Enter where you want to save the folder here" #For example, on a Mac/Users/"username"/Downloads/
#Number of screenshots(Times)
times = 3
#Screenshot interval(Seconds)
span = 1
#Output folder acronym
h_foldername = "Statistical analysis"
#Output file acronym
h_filename = "Document"
#Create output folder(Folder name: Initial_Year, month, day, hour and minute)
folder_name = h_foldername + "_" + str(datetime.datetime.now().strftime("%Y%m%d%H%M"))
path = h_path + folder_name
os.makedirs(path, exist_ok=True)
for i in range(times):
#Give the image a name
filename = h_filename + '_{0:04d}.png'.format(i)+ "_" + str(datetime.datetime.now().strftime("%Y%m%d%H%M%S"))
#Take a screenshot
s = pyautogui.screenshot()
#Save
s.save(folder_name + '/' + filename)
#Screenshot interval(Seconds)
time.sleep(span)
Supplement
I used .format (number) to use 4-digit notation when numbering image files. {0:04d}'.format(number) ・ Fill with 0: 0 ・ 04d: 4 digits
Example(10→0010)
number = 10
number_padded = '{0:04d}'.format(number)
print(number_padded) # => '0010'
Reference
I learned a lot here. [Introduction to Python] Creating a directory easily | os.mkdir ・ os.makedirs [Python] I made a tool to automatically screenshot Kindle books using pyautogui!