Advertisement
goddesschi

cut

Jun 22nd, 2014
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.21 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. default_block_size = 512
  4.  
  5. def slice_file(source_filename, dest_filename, slice_from, slice_to, block):
  6.     f_in = open(source_filename, 'rb')
  7.     f_out = open(dest_filename, 'wb')
  8.     read_count = 0
  9.     eof = False
  10.     while not eof:
  11.         read = f_in.read(block)
  12.         if(len(read) < block): eof = True
  13.         if(read_count < slice_from or read_count >= slice_to):
  14.             f_out.write(read)
  15.         read_count += block
  16.     f_in.close()
  17.     f_out.close()
  18.  
  19.  
  20. if __name__ == '__main__':
  21.     import sys
  22.     if(len(sys.argv) < 3):
  23.         print('missing arguments:')
  24.         print(sys.argv[0] + ' source_filename dest_filename [slicing_starts_from slicing_length [block_size]]')
  25.         quit()
  26.     source_filename = sys.argv[1]
  27.     dest_filename = sys.argv[2]
  28.     if(len(sys.argv) < 4):
  29.         slice_from = int(input('slicing starts from: '))
  30.         slice_to = slice_from + int(input('slicing length: '))
  31.     else:
  32.         slice_from = int(sys.argv[3])
  33.         slice_to = int(sys.argv[3]) + int(sys.argv[4])
  34.     if(len(sys.argv) >= 6):
  35.         block = int(sys.argv[6])
  36.     else: block = default_block_size
  37.     slice_file(source_filename, dest_filename, slice_from, slice_to, block)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement