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.
I'm using two separate scripts, and if possible I would like to combine them. Renamer.py only runs when new photos are added. It sets up the file names for LoginChanger.py. LoginChanger
is picky in the filenames, but Renamer
doesn't change the photos in a loop. So I have to use Renamer
, then LoginChanger
to loop the photos.
Renamer.py
#-------------------------------------------------------------------------------
# Name: Renamer
# Purpose: Renames all .jpg's in a dir from 0 - n (where "n" is the number of .jpg's.)
# This is to be used in conjunction with loginchanger.py
# Author: Nathan Snow
#
# Created: 13/01/2012
# Copyright: (c) Nathan Snow 2012
# Licence: <your licence>
#-------------------------------------------------------------------------------
#!/usr/bin/env python
import os
count = 0
count2 = 0
list1 = []
list2 = []
for filename2 in os.listdir('.'):
if filename2.endswith('.jpg'):
list1.append(filename2)
while count < len(list1):
os.rename(filename2, str(count)+"old.jpg")
count += 1
for filename3 in os.listdir('.'):
if filename3.endswith('.jpg'):
list2.append(filename3)
while count2 < len(list2):
os.rename(filename3, str(count2)+".jpg")
count2 += 1
LoginChanger.py
#-------------------------------------------------------------------------------
# Name: LoginChanger
# Purpose: This is to be used after renamer.py. This loops the properly named
# .jpg's in a "circle" first to last, 2-1, 3-2, ..., and changes
# 0 to BackgroundDefault.jpg.
# Author: Nathan Snow
#
# Created: 13/01/2012
# Copyright: (c) Nathan Snow 2012
# Licence: <your licence>
#-------------------------------------------------------------------------------
#!/usr/bin/env python
import os
count = 0
list1 = []
list2 = []
list3 = []
for filename in os.listdir('.'):
if filename.endswith('.jpg'):
list3.append(filename)
list3len = len(list3)
list3len = list3len - 1
if filename.startswith('BackgroundDefault'):
os.rename('BackgroundDefault.jpg', str(list3len)+'.jpg')
for filename2 in os.listdir('.'):
if filename2.endswith('.jpg'):
list1.append(filename2)
while count < len(list1):
jpg = str(count)+'.jpg'
oldjpg = str(count)+'old.jpg'
os.rename(str(jpg), str(oldjpg))
count += 1
for filename4 in os.listdir('.'):
if filename4.startswith('0'):
os.rename('0old.jpg', 'BackgroundDefault.jpg')
count = 1
count2 = 0
for filename3 in os.listdir('.'):
if filename3.endswith('.jpg'):
list2.append(filename3)
while count < len(list2):
newjpg = str(count2)+'.jpg'
oldjpg = str(count)+'old.jpg'
os.rename(str(oldjpg), str(newjpg))
count2 += 1
count += 1
print ('Created new login background')