''' Adds line numbers to all the lines in a .txt files. Written By Sunjay Varma ''' def lineNumbers(fname, widthIndex=3): # open the file. f = open(fname, 'r') # 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 + ". " + line) # incrment num num += 1 # close the file f.close() # write the new lines to the .txt file f = open(fname, 'w') f.write(''.join(nlines)) f.close() if __name__ == "__main__": filename = raw_input("What is the file path of your .txt file? ") width = raw_input("What is the maximum number of 0s before the line number? ") lineNumbers(filename, width)