-2

How to chunk a file of any type into pieces and then converted it into string with the help of python?

I need to create a web app which runs on php as backend and when i upload a file in it... For safety it needs to split into N pieces of equal sizes and then convert it into strings so that it will be easy to transfer it to other storage drive

Santhosh Kumar
  • 543
  • 8
  • 22
  • 2
    Please explain your problem in more details. – Klaus D. Mar 26 '16 at 15:28
  • I need to create a web app which runs on php as backend and when i upload a file in it... For safety it needs to split into N pieces of equal sizes and then convert it into strings so that it will be easy to transfer it to other storage drive – Santhosh Kumar Mar 26 '16 at 15:33

1 Answers1

1

EDIT: For your updated question, it might be the easiest to use a library suited for this problem - requests comes to mind here

Here's a chunkify from Splitting a list of into N parts of approximately equal length

def chunks(l, n):
    """ Yield successive n-sized chunks from l.
    """
    for i in range(0, len(l), n): # Use xrange if you're using Python 2 - it won't create the range list.
        yield l[i:i+n]

And to convert that to a string you could just use:

":".join(",".join(str(elem) for elem in chunk) for chunk in chunks(l, n))

For l = [1, 2, 3, 4, 5, 6] this prints:

>>> "1,2,3:4,5,6"
Community
  • 1
  • 1
Bahrom
  • 4,752
  • 32
  • 41