Forum Discussion

Michael82353's avatar
Michael82353
Copper Contributor
Sep 26, 2024

screenshots program

I wrote a program using Python about a few moths ago to re-number my screenshots directory. I figured I would like to compensate for deletions in the default screenshot naming/numbering. After perfecting the program I've found that the screenshot function continues to name/number new screenshots from the latest screenshot ever existing.

 

For example, I had 25 screenshots, and deleted 16, 18, and 20 throughout the week. I run my program and it renames all of them using the default title 'Screenshot ().png', and I then have screenshots 1-22.

The issue:

When I create the next new screenshot, it's created as 'Screenshot (26).png' by default, instead of 'Screenshot (23).png'.

 

I'm still using Windows 10 Pro, so I'm unsure whether this is a feature of Windows 11. This should be a regular feature implemented in the OS.

Does anyone know how I might be able to reset the count each time I run my program?

Code added below:

import os
import re


def Numbering_Screenshots():

src="C:\\Users\\{username}\\OneDrive\\Pictures\\Screenshots"
dest = "C:\\Users\\{username}\\OneDrive\\Pictures\\Screenshots\\"


try:
os.chdir(src)
directory = os.listdir()

directory = sorted(directory, key=lambda x: int(re.search(r'\d+', x).group()))

for img in directory:

new_filename = f"Screenshot ({directory.index(img) + 1}).png"

if img == new_filename:
continue

else:

os.rename(img, dest + new_filename)

print("Renumbering Completed.")
except Exception as e:
print(f"An error occured: {e}")

if __name__ == "__main__":
Numbering_Screenshots()

Resources