Python Forum
simple for loop? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: simple for loop? (/thread-29828.html)



simple for loop? - gr3yali3n - Sep-21-2020

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')



RE: simple for loop? - Yoriz - Sep-21-2020

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



RE: simple for loop? - Mik3e - Sep-22-2020

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


RE: simple for loop? - buran - Sep-22-2020

@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.