lolamontes69

Python / File splitter.

May 29th, 2013
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.14 KB | None | 0 0
  1. import os as os
  2. def write_new(outputfn, countfn):
  3.     outputfn1 = outputfn + str(countfn) + ".txt"
  4.     text_file = open(outputfn1, "w")
  5.     return text_file
  6.  
  7. def main():
  8.     os.system('clear')
  9.     print "<<<Using full name including suffix(.txt etc)>>>\n"
  10.     filename = str(raw_input("What is the input filename? >"))
  11.     limit = str(raw_input('How many lines should I split it to? > '))
  12.     os.system('clear')
  13.     print "<<<Just type a name and I'll save them as .txt files>>>\n"
  14.     outputfn = str(raw_input("What do you want to call the output files?"))
  15.     outputfn1 = outputfn + "0.txt"
  16.     fin = open(filename)
  17.     count, countfn = 1, 0
  18.     text_file = open(outputfn1, "w")
  19.     for line in fin:
  20.         line1 = line.strip()
  21.         if int(count) == int(0):
  22.             try: text_file.close()
  23.             except: pass
  24.             text_file = write_new(outputfn, countfn)
  25.             countfn += 1
  26.         text_file.write(line1)
  27.         text_file.write("\n")
  28.         count += 1
  29.         if count == int(limit): count = 0
  30.     text_file.close()
  31.     os.system('clear')
  32.     print "Your files are in the same folder as this program"
  33.  
  34. main()
Advertisement
Add Comment
Please, Sign In to add comment