Python Forum

Full Version: simple for loop?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
how would I write a for loop to take 15 screenshots under different names with pyautogui or any other way? the pyautogui just replaces the same picture over and over.
I thought I had these for loops understood but apparently not , I still have a lot to learn.
I have another for loop question but ill leave this to one question.

ive tried

for pic in range(15):
    pyautogui.screenshot('/Users/user/Desktop/screenshots/picture.png')
This will increment picture1..2..3..4 etc
for pic in range(15):
    pyautogui.screenshot(f'/Users/user/Desktop/screenshots/picture{pic}.png')
Here's another technique:
import os

filelist = os.listdir("/media/Avocet/Python/")
for filename in filelist:
    print(filename)
The output (on my computer) is:
Quote:testing.py
CheckTest.py
tkinter.pdf
CounterTest.py
ForTest.py
passweird.py
alarming.py
Colors.odt
PythonNotes.odt
hoot.wav
says.wav

I imagine your screenshot line will look something like this:
pyautogui.screenshot(f'/Users/user/Desktop/screenshots/' + filename)
One advantage of this technique is that it works with whatever files exist. Disclaimer, I didn't test the screenshot part.
Mike
@Mik3e, I think you misunderstood OP request. They want to take 15 screenshots and save each one under different name. They don't try to list files already existing in a folder.