Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
simple for loop?
#1
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')
Reply
#2
This will increment picture1..2..3..4 etc
for pic in range(15):
    pyautogui.screenshot(f'/Users/user/Desktop/screenshots/picture{pic}.png')
Reply
#3
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
Reply
#4
@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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Seemingly simple loop Weber585 7 3,434 Mar-21-2021, 07:19 PM
Last Post: jefsummers
  Simple Variable Saving in Loop DevDev 3 2,959 Mar-09-2021, 07:17 PM
Last Post: Fre3k
  Adding second message to simple loop error PythonGainz 2 2,054 Apr-06-2020, 11:55 AM
Last Post: PythonGainz
  simple key value loop help siten0308 4 2,390 Jun-25-2019, 02:53 PM
Last Post: siten0308
  Simple while loop only works on first attempt jsb83 2 1,989 Jun-20-2019, 08:57 PM
Last Post: jsb83

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020