file i/o and the the meaning of binary modes
Question: so I want to save some arbitrary piece of data in redis with python. Since
redis supports this by just storing it as a string I thought I could read the
date with python again and write it to a file. At first this didn`t work
because I used the standard 'r' and 'w' mode of open(). Python does say they
are equal though.

After I changed it to 'rb' and 'wb' it works, but why does the non binary read
or write change the data somehow? what is the significance of it anyway?

here is some code which works, but just change the file modes to non-binary
and watch the testfile_read.zip change. You do need redis though, easy to
install with pip install redis

    
    
    import redis
    import os.path
    
    version=1.0
    path='testfile.zip'
    
    r_server=redis.Redis("127.0.0.1")
    fp = open(path,'rb')
    
    test=fp.read()
    fp.close()
    
    r_server.hset('testfile',version,test)
    r_server.hset('testfile','currver',version)
    
    
    test2=r_server.hget('testfile',version)
    
    if test==test2:
        print "read from file and read from redis are the same"
    else:
        print "read from file and read from redis are the NOT!! same"
    
    fp2 = open("testfile_read.zip",'wb')
    fp2.write(test2)
    fp2.close()
    
    fp3 = open("testfile_read.zip",'rb')
    test3=fp3.read()
    fp3.close()
    
    if test2==test3:
        print "redis is equal to written file"
    else:
        print "redis is NOT!!! equal to written file"
    


Answer: The reason for having non-binary mode is to have the file calls do some
manipulation of the data for you (for example, translating line-endings).
That's why you don't have to worry so much about where a text file came from;
the file routines hide those details from you.

