#!/usr/bin/env python ###################################################### # Removes or adds line numbers from a .txt file # Leave newName as None to write to the .txt file # # Written by Sunjay Varma # Check out www.sunjay-varma.com for more cool stuff ###################################################### DEFAULT_PREFIX = ". " import sys ## Adds line numbers to the contents of a .txt file. def addLNumbers(filename, newName=None, widthIndex=None, prefix=None): # first open the filename f = open(filename, 'r') # fix up the widthIndex widthIndex = widthIndex or 3 # fix up the prefix prefix = prefix or DEFAULT_PREFIX # The list where the newly formatted lines will go. nlines = [] # The number to add to the line. num = 1 # go through the lines in the .txt file for line in f.readlines(): # add a formatted line to nlines nlines.append(("%"+("."+str(widthIndex)+"i"))%num + prefix + line) # incrment num num += 1 # close the file f.close() # get the new file name newName = newName or filename # write the new lines newfile = open(newName, 'w') newfile.write(''.join(nlines)) newfile.close() # Removes line numbers from a the contents of a .txt file. def removeLNumbers(filename, newName=None, prefix=None): # first open the filename f = open(filename, 'r') # fix up the prefix prefix = prefix or DEFAULT_PREFIX # a list of the tranfered lines. nlines = [] # go through all the lines for line in f.readlines(): # fix the line try: nlines.append(line[line.index(prefix)+1:]) except: continue # line must not have the prefix... # close the file f.close() # get the new file name newName = newName or filename # write the new lines newfile = open(newName, 'w') newfile.write(''.join(nlines)) newfile.close() if __name__ == '__main__': if raw_input("Type add if you want to add line numbers and remove if you want to remove them:\n\t").lower().startswith('add'): # get the file name fname = raw_input("Enter the complete file path of the file you want to convert:\n\t") if raw_input("Would you like to write to a new file? ").lower().startswith('y'): nname = raw_input("Please enter the new file path. ") else: nname=None if raw_input("Is there a specific prefix you want after each line number? ").lower().startswith('y'): prefix = str(raw_input("Please enter the prefix: ")) else: prefix = None if raw_input("Is there a maximum amount of 0s you would like to use? ").lower().startswith('y'): index = input("Please enter the max width before each line number:\n\t") else: index = None still_running = 1 try: addLNumbers(fname, nname, index, prefix) except: still_running = 0 for item in sys.exc_info(): print item raw_input("Press Enter to exit.") if still_running: print "" print "Your file has had line numbers added to it sucessfully." print "New File Location: ", nname or fname print "WidthIndex: ", index or "3 (Default)" print "The prefix used: \""+(prefix or DEFAULT_PREFIX)+"\"" print "" print "Thank you for using LineNumConverter.py" print "Check out www.sunjay-varma.com for more stuff." print "" print "Bye Bye!" raw_input("Press Enter to exit") else: # get the file name fname = raw_input("Enter the complete file path of the file you want to convert:\n\t") if raw_input("Would you like to write to a new file? ").lower().startswith('y'): nname = raw_input("Please enter the new file path. ") else: nname=None if raw_input("Is there a specific prefix after each line number? ").lower().startswith('y'): prefix = raw_input("Please enter the prefix: ") else: prefix = None still_running = 1 try: removeLNumbers(fname, nname, prefix) except: still_running = 0 for item in sys.exc_info(): print item raw_input("Press Enter to exit.") if still_running: print "" print "Your file has been sucessfully cleaned of line numbers." print "New File Location: ", nname or fname print "The prefix used: \""+(prefix or DEFAULT_PREFIX)+"\"" print "" print "Thank you for using LineNumConverter.py" print "Check out www.sunjay-varma.com for more stuff." print "" print "Bye Bye!" raw_input("Press Enter to exit")