×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Python
Posted by: M. Chris
Added: Apr 24, 2019 9:05 AM
Views: 3877
Tags: no tags
  1. #!/usr/bin/python
  2. #########################################################
  3. # split a file into a set of portions; join.py puts them
  4. # back together; this is a customizable version of the
  5. # standard unix split command-line utility; because it
  6. # is written in Python, it also works on Windows and can
  7. # be easily tweaked; because it exports a function, it
  8. # can also be imported and reused in other applications;
  9. #########################################################
  10.  
  11. import sys, os
  12. kilobytes = 1024
  13. megabytes = kilobytes * 1000
  14. chunksize = int(1.4 * megabytes)                   # default: roughly a floppy
  15.  
  16. def split(fromfile, todir, chunksize=chunksize):
  17.     if not os.path.exists(todir):                  # caller handles errors
  18.         os.mkdir(todir)                            # make dir, read/write parts
  19.     else:
  20.         for fname in os.listdir(todir):            # delete any existing files
  21.             os.remove(os.path.join(todir, fname))
  22.     partnum = 0
  23.     input = open(fromfile, 'rb')                   # use binary mode on Windows
  24.     while 1:                                       # eof=empty string from read
  25.         chunk = input.read(chunksize)              # get next part <= chunksize
  26.         if not chunk: break
  27.         partnum  = partnum+1
  28.         filename = os.path.join(todir, ('part%04d' % partnum))
  29.         fileobj  = open(filename, 'wb')
  30.         fileobj.write(chunk)
  31.         fileobj.close()                            # or simply open(  ).write(  )
  32.     input.close(  )
  33.     assert partnum <= 9999                         # join sort fails if 5 digits
  34.     return partnum
  35.            
  36. if __name__ == '__main__':
  37.     if len(sys.argv) == 2 and sys.argv[1] == '-help':
  38.         print 'Use: split.py [file-to-split target-dir [chunksize]]'
  39.     else:
  40.         if len(sys.argv) < 3:
  41.             interactive = 1
  42.             fromfile = raw_input('File to be split? ')       # input if clicked
  43.             todir    = raw_input('Directory to store part files? ')
  44.         else:
  45.             interactive = 0
  46.             fromfile, todir = sys.argv[1:3]                  # args in cmdline
  47.             if len(sys.argv) == 4: chunksize = int(sys.argv[3])
  48.         absfrom, absto = map(os.path.abspath, [fromfile, todir])
  49.         print 'Splitting', absfrom, 'to', absto, 'by', chunksize
  50.  
  51.         try:
  52.             parts = split(fromfile, todir, chunksize)
  53.         except:
  54.             print 'Error during split:'
  55.             print sys.exc_type, sys.exc_value
  56.         else:
  57.             print 'Split finished:', parts, 'parts are in', absto
  58.         if interactive: raw_input('Press Enter key') # pause if clicked