Counter in python

0

Well I'm doing a screenshot program but I want it to replace in the file name when saving, the characters "XX", by the print number. Ex: ScreenShotXX.jpg in "XX" I want to put the print number ex: 01, 02 Code used:

import pyscreenshot as ImageGrab

def main():
    image = ImageGrab.grab()

    cont = 1

    for c in cont:

        c =+ 1

        image.save('screenShot' + str(c) + '.jpg', 'jpeg')


if __name__ == '__main__':
    main()

I'm using python 2.7

    
asked by anonymous 11.02.2017 / 15:16

2 answers

0

You should only access the folder where Screenshots are being saved, check if there are files in it, if yes, get the last file after ordering, and extract the XX of the name of that last photo, just add +1 and save the new capture, but if the folder is empty, just consider the value 01 because it would be the first capture.

    
11.02.2017 / 16:20
4

Your code is not very clear. You define a variable cont with the value 1 and then use a c variable in a loop (without using range ), but increment it inside the loop .....?

If you know the number of catches you want, put that number inside the cont variable and do so:

cont = 10

for c in range(cont):

    file = 'screenShot{:05d}.jpg'.format(c)

    print(file)

Result (see working on Ideone ):

screenShot00000.jpg
screenShot00001.jpg
screenShot00002.jpg
screenShot00003.jpg
screenShot00004.jpg
screenShot00005.jpg
screenShot00006.jpg
screenShot00007.jpg
screenShot00008.jpg
screenShot00009.jpg
The 05d fault mask used in the format call says that the number is integer ( d ), length of 5 characters with 0 on the left ( 05 - if using only 5 there will be spaces at left).

That is, your final code looks like this:

import pyscreenshot as ImageGrab

def main():
    cont = 10 # Número de capturas

    for c in range(cont):

        # Desnecessário
        # c =+ 1 

        image = ImageGrab.grab()
        image.save('screenShot{:05d}.jpg'.format(c), 'jpeg')


if __name__ == '__main__':
    main()
    
11.02.2017 / 16:41