title: Creating a Photo Gallery using Python
body: <p>I'm a novice programmer and thought it would be a fun learning experience to create a photo gallery using python.  I've gotten pretty far in the project, but recently got stuck.</p>
  
  <p>I have a folder filled with photos.  I was able to generate an index page with thumbnails.  When I click on a thumbnail, a larger version appears.  However, when someone clicks the larger version, I'd like it to go to the next photo.  Right now, the user has to click back to the index page to get to the next photo.  Here's the index page with working thumbnails.</p>
  
  <p><a href="http://dl.dropbox.com/u/26085098/CCC%20Culinary%20Food%20and%20Wine%20Event%202011/index.html" rel="nofollow">http://dl.dropbox.com/u/26085098/CCC%20Culinary%20Food%20and%20Wine%20Event%202011/index.html</a></p>
  
  <p>The python script I used to create the gallery is shown below. </p>
  
  <p>I would love if someone could point me in the right direction.  Also, any suggestions on making my code more elegant would be greatly appreciated.</p>
  
  <pre><code>import os
  
  index=os.listdir('./Images')
  
  x=len(index)
  
  for fname in index:
      while x&gt;0:
          x=x-1
          index[x] = '&lt;a href="./' + index[x].replace("jpg", "html") + '"&gt;' + '&lt;img src="./Thumbs/' + index[x] + '" /&gt;' + '&lt;/a&gt;'
  
  listString='\n'.join(index)
  
  title=os.getcwd()
  title=title.split("/")
  title=title.pop()
  
  file = open("index.html", 'w')
  
  file.write('&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"' + '\n')
  file.write('    "http://www.w3.org/TR/html4/loose.dtd"&gt;' + '\n')
  file.write('&lt;html&gt;' + '\n')
  file.write('&lt;title&gt;' + title + '&lt;/title&gt;' + '\n')
  file.write('&lt;head&gt;' + '\n')
  file.write('&lt;style&gt;' + '\n')
  file.write('body {padding:10px;background-color:black;margin-left:15%;margin-right:15%;font-family:"Lucida Grande",Verdana,Arial,Sans-Serif;color: white;}' + '\n')
  file.write('img {border-style:solid;border-width:5px;border-color:white;}' + '\n')
  file.write('&lt;/style&gt;' + '\n')
  file.write('&lt;/head&gt;' + '\n')
  file.write('&lt;body&gt;' + '\n')
  file.write('&lt;h1&gt;' + title + '&lt;/h1&gt;' + '\n')
  file.write(listString + '\n')
  file.write('&lt;/body&gt;' + '\n')
  file.write('&lt;/html&gt;')
  
  file.close()
  
  
  next=os.listdir('./Images')
  
  x=len(next)
  
  for name in next:
      while x&gt;0:
          x=x-1
          next[x] = next[x].replace("jpg", "html")
  
  image=os.listdir('./Images')
  page=os.listdir('./Images')
  
  x=len(page)
  
  for fname in page:
      while x&gt;0:
          x=x-1
          page[x] = page[x].replace("jpg", "html")
          file = open(page[x], 'w')
          file.write('&lt;a href="./' + next[x] + '"&gt;' + '&lt;img height="95%" src="./Images/' + image[x] + '" /&gt;' + '&lt;/a&gt;')
          file.close()
  </code></pre>
  
  <p>I tried making the next url show up by incrementing "next", but it gives me an error.</p>
  
  <pre><code>next[x] = next[x+1].replace("jpg", "html")
  IndexError: list index out of range
  </code></pre>