From 673894343b070ecfbf3e69a27f61dd8c09758373 Mon Sep 17 00:00:00 2001 From: Eduardo Robles Elvira Date: Fri, 27 Sep 2013 16:04:28 +0200 Subject: [PATCH] improving the filesplitter, allowing to split at specified offsets. this is useful for debugging or to use as a last resort technique to recover a backup, knowing the offsets from the index for example --- filesplit.py | 24 +++++++++++++++++++----- 1 files changed, 19 insertions(+), 5 deletions(-) diff --git a/filesplit.py b/filesplit.py index 00060ec..2bddd2a 100644 --- a/filesplit.py +++ b/filesplit.py @@ -75,14 +75,28 @@ def split_file(separator, prefix, input_file, new_file_func=None): output.close() +def chunk_file(input_file, output_file, from_pos, to_pos): + ifl = open(input_file, 'r') + ofl = open(output_file, 'w') + + ifl.seek(from_pos) + ofl.write(ifl.read(to_pos-from_pos)) + ifl.close() + ofl.close() + if __name__ == "__main__": parser = argparse.ArgumentParser() - parser.add_argument("-s", "--separator", required=True, - help="string for the separator") - parser.add_argument("-p", "--prefix", required=True, - help="prefix for split files") + parser.add_argument("-s", "--separator", help="string for the separator") + parser.add_argument("-p", "--prefix", help="prefix for split files") parser.add_argument("input_file", help="input file") + parser.add_argument("-f", "--from-pos", type=int, default=-1) + parser.add_argument("-t", "--to-pos", type=int, default=-1) + parser.add_argument("-o", "--output", default=None) args = parser.parse_args() - split_file(separator=args.separator, prefix=args.prefix, input_file=args.input_file) + if args.from_pos > -1 and args.to_pos > -1: + chunk_file(input_file=args.input_file, output_file=args.output, + from_pos=args.from_pos, to_pos=args.to_pos) + else: + split_file(separator=args.separator, prefix=args.prefix, input_file=args.input_file) -- 1.7.1