#!/usr/bin/python from sys import exit, argv import os, tempfile, shutil def parseDirectory(dir_name): os.chdir(dir_name) # cd into the directory files = os.listdir(dir_name) for f in files: if os.path.isfile(f): parseFile(f) print "Completed: " + dir_name def parseFile(file_name): file_base, file_ext = os.path.splitext(file_name) # fills file_base with the text filename without the extension, and file_ext with the extention if file_name.endswith(".html"): # endswith check the suffix of a string file_dash = file_base.rfind("-") # rfind checks for a string in reverse file_index = file_base[file_dash+1:] # +1 is needed to strip the dash from the string added_code = 0 # boolean value used to determine when to place the link text if file_index.isdigit(): # if the suffix is not a digit we do not want to process the file index_len = len(file_index) # the length here will be used to strip the value before adding the previous and next index numbers prev_index = int(file_index) - 1 next_index = int(file_index) + 1 prev_link = '
Previous
\n' next_link = '
Next
\n' f = file(file_name,"r") g = file(y,"w") # opens a temporary file for writing, clearing the file contents as well for line in f: g.write(line) if added_code: # this value will be true after the " try: print "Dir: " + str(argv[1]) if os.path.isdir(argv[1]): input_dir = os.path.abspath(argv[1]) x, y = tempfile.mkstemp() # creates a temporary file saved in y parseDirectory(input_dir) except IndexError: Usage("Invalid Arguments")