How to output integer arrays to file in python?
Question: I have 3000000 ints' long array which I want to output to a file. How can I do
that? Also, is this

    
    
    for i in range(1000):
        for k in range(1000):
            (r, g, b) = rgb_im.getpixel((i, k))
            rr.append(r)
            gg.append(g)
            bb.append(b)
    d.extend(rr)
    d.extend(gg)
    d.extend(bb)
    

a good practice to join array together?

All of the arrays are declared like this `d = array('B')`

EDIT: Managed to output all int`s delimited by ' ' with this

    
    
    from PIL import Image
    import array
    
    side = 500
    
    for j in range(1000):
        im = Image.open(r'C:\Users\Ivars\Desktop\RS\Shape\%02d.jpg' % (j))
        rgb_im = im.convert('RGB')
        d = array.array('B')
        rr = array.array('B')
        gg = array.array('B')
        bb = array.array('B')
        f = open(r'C:\Users\Ivars\Desktop\RS\ShapeData\%02d.txt' % (j), 'w')
        for i in range(side):
            for k in range(side):
                (r, g, b) = rgb_im.getpixel((i, k))
                rr.append(r)
                gg.append(g)
                bb.append(b)
        d.extend(rr)
        d.extend(gg)
        d.extend(bb)
        o = ' '.join(str(t) for t in d)
        print('#', j, ' - ', len(o))
        f.write(o)
        f.close()
    


Answer: if you're using python >= 2.6 then you can use print functions from the
_future_!

    
    
    from __future__ import print_function
    
    #your code
    
    # This will print out a string representation of list to the file.
    # If you need it formatted differently, then you'll have to construct the string yourself
    print(d, file=open('/path/to/file.txt','w')
    
    #you can join the list items with an empty string to get only the numbers
    print("".join(d),file=('/path/to/file.txt','w'))
    

This has the side effect of turning print from a statement into a function, so
you'll have to wrap whatever you want printed in ()

