HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Photo changer loop

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
loopphotochanger

Problem

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:     
#-------------------------------------------------------------------------------
#!/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 Backgr

Solution


  • Your variable names are pretty bad: filename1, filename2, filename3, ... is bad, use something more meaningful.



  • There is no need to use different loop variables in consecutive loops. Use filename everytime.



  • You need to move the #!/usr/bin/env python shebang line to the first line of the file or it won't work at all.



  • You have unnecessary str() calls in os.rename(str(oldjpg), str(newjpg)) - both variables are already strings.

Context

StackExchange Code Review Q#7809, answer score: 3

Revisions (0)

No revisions yet.