code: def copy_file(stream, target, maxread=-1, buffer_size=2*16):
      ''' Read from :stream and write to :target until :maxread or EOF. '''
      size, read = 0, stream.read
      while 1:
          to_read = buffer_size if maxread < 0 else min(buffer_size, maxread-size)
          part = read(to_read)
          if not part:
              return size
          target.write(part)
          size += len(partf copy_file(stream, target, maxread=-1, buffer_size=2*16):
      ''' Read from :stream and write to :target until :maxread or EOF. '''
      size, read = 0, stream.read
      while 1:
          to_read = buffer_size if maxread < 0 else min(buffer_size, maxread-size)
          part = read(to_read)
          if not part:
              return size
          target.write(part)
          size += len(part)
docs: Read from :stream and write to :target until :maxread or EOF.