Code Snippets

Internet Shortcuts to HTML file – Python

I had problems with the DOS batch file version of this utility so decided to replace it with a more robust version written in Python.

import os
import time
import datetime

# Converting all url files in directory and sub-directories to a single html file

tim =
tim1 = tim.strftime("%d-%m-%Y")
day = tim.strftime("%d")
mth = tim.strftime("%m")
year = tim.strftime("%Y")

tim2 = tim.strftime("%H:%M:%S")
nameOutput = "Links"+day+mth+year+".html"
nameList = "Links"+day+mth+year+"_list.txt"
print 'Generating list ' + nameOutput +' at '+tim2+' on '+tim1

dir_path = os.getcwd()
listFile = open(nameList, 'w')

with open(nameOutput, 'w') as outputFile:
  outputFile.write( '<h1>List Generated at '+tim2+' on ' + tim1 +'</h1>')

  for dirName, subDirs, files in os.walk(dir_path):
    outputFile.write( '<h3>'+dirName+'</h3>\n' )
    outputFile.write( '<ol>\n' )
    for name in files:
      if name.endswith(".url"):
        with open(dirName+'\\'+name) as fh:
          base, ext = os.path.splitext(name)
          for line in fh:
            if line.startswith("URL="):
              href = line.split("URL=",1)[1].rstrip('\n')
              outputFile.write( '<li><a href="'+href+'" target="_blank">'+base+'</a></li>\n' )
              listFile.write( href+'\n' )
    outputFile.write( '</ol>\n' )


When invoked, this version will go through the directory and all sub-directories to generate an html file of all the URLs it finds. It will also create a list of the URLs in a separate file.