peter peter -4 years ago 157
Python Question

read whole file at once

I need to read whole source data from file something.zip (not uncompress it)

I tried

f = open('file.zip')
s = f.read()
f.close()
return s


but it returns only few bytes and not whole source data. Any idea how to achieve it? Thanks

Answer Source

Use binary mode(b) when you're dealing with binary file.

def read_zipfile(path):
    with open(path, 'rb') as f:
        return f.read()

BTW, use with statement instead of manual close.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download