I just started python a few days ago, and I haven't programmed much before. I know my code is terrible; however, I would like someone to look it over. What I'm trying to do is create a photo "loop" that changes my Windows 7 login screen. I had a batch file doing it, but even though my code is crappy it does the job way better.
Here is my code, however it limits the number of photos I can have to 9. If I have 10 or more photos, it starts failing and renaming things wrong.
LoginChanger.py
#!/usr/bin/env python
import os
list1 = []
count1 = 0
# Finds jpg files and makes list.
for filename in os.listdir('.'):
if filename.endswith(('.jpg', '.JPG', 'jpeg', '.JPEG', '.Jpg', '.Jpeg')):
list1.append(filename)
list1end = len(list1)
# Finds background file and sends it to the back of the list.
if filename.startswith(('BackgroundDefault', 'backgrounddefault')):
# If name already exists for some reason, makes old file.
if str(list1end - 1)+'.jpg' in list1:
os.rename('BackgroundDefault.jpg', 'OldBackground.jpg')
else:
os.rename('BackgroundDefault.jpg', str(list1end - 1)+'.jpg')
# Resets The list.
list1 = []
# Creates a list with all jpgs.
for filename in os.listdir('.'):
if filename.endswith(('.jpg', '.JPG', 'jpeg', '.JPEG', '.Jpg', '.Jpeg')):
list1.append(filename)
list1end = len(list1)
# Renames the beginning object of list to background file.
os.rename(list1[0], 'BackgroundDefault.jpg')
# Renames all files incrementally.
while count1 < list1end - 1:
oldname = list1[count1 + 1]
os.rename(oldname, str(count1)+'.jpg')
count1 += 1
print ('Login Background Has Been Changed.')