Python - Create html page with hyperlinks from os.listdir
<p>I have a script that creates a folder called "videos" on a USB drive, moves 6,500 WMV files over to the "videos" folder. Then it's suppose to create an HTML page with hyperlinks to each file. Here is my current example that's broken. I'm trying to have it crawl the videos directory and create an HTML page with hyperlinks only to the local files on the USB drive.</p>

<pre><code>#!/usr/bin/python
import os.path
import os
import shutil
import re

# Create the videos directory in the current location
# If the directory exists ignore it
def createDirectory():
    directory = "videos"
    if not os.path.isdir("./" + directory + "/"):
        os.mkdir("./" + directory + "/")
        print "Videos Folder Created."
    else:
        print "Video Folder Exists."
        print "---------------------"

# Move all the files in the root directory with the .wmv extension
# to the videos folder
def moveVideos():
    for file in os.listdir("."):
        if os.path.splitext(file)[1] == ".wmv":
            print "Moving:", file
            shutil.move(file, os.path.join("videos", file))

def createHTML():
    videoDirectory = os.listdir("videos")
    f = open("videos.html", "w")
    f.writelines(videoDirectory)
    r = re.compile(r"(\\[^ ]+)")
    print r.sub(r'&lt;a href="\1"&gt;\1&lt;/a&gt;', videoDirectory)

createDirectory()
moveVideos()
createHTML()
</code></pre>
