ToKeiChun

File Splitter

Sep 12th, 2020 (edited)
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.08 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import sys
  5. import os
  6.  
  7. class FileSplitter:
  8.  
  9.     def __init__(self):
  10.         self.parse_args(sys.argv)
  11.  
  12.     @staticmethod
  13.     def run():
  14.         splitter = FileSplitter()
  15.         splitter.split()
  16.  
  17.     def split(self):
  18.  
  19.         file_number = 1
  20.         line_number = 1
  21.  
  22.         print ("Splitting %s into multiple files with %s lines" % (os.path.join(self.working_dir, self.file_base_name+self.file_ext), str(self.split_size)
  23. ))
  24.  
  25.         out_file = self.get_new_file(file_number)
  26.         for line in self.in_file:
  27.             out_file.write(line)
  28.             line_number += 1
  29.             if line_number == self.split_size:
  30.                 out_file.close()
  31.                 file_number += 1
  32.                 line_number = 1
  33.                 out_file = self.get_new_file(file_number)
  34.  
  35.         out_file.close()
  36.  
  37.         print ("Created %s files." % (str(file_number)))
  38.  
  39.     def get_new_file(self,file_number):
  40.         """return a new file object ready to write to"""
  41.         new_file_name = "%s.%s%s" % (self.file_base_name, str(file_number), self.file_ext)
  42.         new_file_path = os.path.join(self.working_dir, new_file_name)
  43.         print ("creating file %s" % (new_file_path))
  44.         return open(new_file_path, 'w')
  45.  
  46.     def parse_args(self,argv):
  47.         """parse args and set up instance variables"""
  48.         try:
  49.             self.split_size = 1000
  50.             if len(argv) > 2:
  51.                 self.split_size = int(argv[2])
  52.             self.file_name = argv[1]
  53.             self.in_file = open(self.file_name, "r")
  54.             self.working_dir = os.getcwd()
  55.             self.file_base_name, self.file_ext = os.path.splitext(self.file_name)
  56.         except Exception as e:
  57.             print(e)
  58.  
  59.     def usage(self):
  60.         return ("""
  61.        
  62.        Usage:
  63.  
  64.            $ python file_splitter.py <file_name> nomor per baris
  65.  
  66.        """)
  67.  
  68. if __name__ == "__main__":
  69.     os.system('cls' if os.name == 'nt' else 'clear')
  70.  
  71.     print("""
  72.    [+] Lust - Serizawa
  73.    [+] Expl0it th1s w0rld
  74.    """)
  75.     FileSplitter.run()
Add Comment
Please, Sign In to add comment