Created
April 24, 2019 17:51
-
-
Save technicalflaw/2bf24ba46d3780dbeeb7d4a1d315f8a7 to your computer and use it in GitHub Desktop.
Revisions
-
technicalflaw created this gist
Apr 24, 2019 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,90 @@ #! /usr/bin/env python """ Split a Unix-style mailbox into individual files Written by Aquarius <[email protected]> Usage: splitmbox.py <mailbox-file> <directory> This will create files numbered 01,02,03... in <directory>. The number of prefixed zeroes will make all filenames in the directory the same length. """ import mailbox,sys,getopt,string,os VERSION = '0.1' USAGE = """Usage: splitmbox.py [ OPTION ] <mailbox-file> <directory> -h, --help Show this help text and exit -v, --version Show file version and exit This will create files numbered 01,02,03... in <directory>. The number of prefixed zeroes will make all filenames in the directory the same length. """ try: optlist, args = getopt.getopt(sys.argv[1:],'hv',['help','version']) except getopt.error, error_text: print error_text,'\n' print USAGE sys.exit(1) for tuple in optlist: if tuple[0] == '-h' or tuple[0] == '--help': print USAGE sys.exit(0) if tuple[0] == '-v' or tuple[0] == '--version': print VERSION sys.exit(0) if len(args) != 2: print USAGE mbox_fname, output_dir = args if output_dir[-1] != '/': output_dir = output_dir + '/' # Make the output directory, if required try: os.mkdir(output_dir) except os.error,ertxt: if string.find(str(ertxt),'File exists') == -1: print 'Failed to create or use directory',output_dir,'[',ertxt,']' sys.exit(1) try: mbox_file = open(mbox_fname) except: print "Failed to open file",mbox_fname sys.exit(1) mbox = mailbox.UnixMailbox(mbox_file) # Find out how many messages in the mailbox count = 0 while 1: msg = mbox.next() if not msg: break count = count + 1 # Now do it again, outputting files mbox_file.close() mbox_file = open(mbox_fname) mbox = mailbox.UnixMailbox(mbox_file) digits = len(str(count)) count = 0 while 1: msg = mbox.next() if not msg: break count = count + 1 fname = output_dir+('0'*digits+str(count))[-digits:] outfile = open(fname,'w') for s in msg.headers: outfile.write(s) outfile.write('\n') for s in msg.fp.readlines(): outfile.write(s) outfile.close()