diff --git a/ox/cache.py b/ox/cache.py index 4137a36..123ec96 100644 --- a/ox/cache.py +++ b/ox/cache.py @@ -119,9 +119,8 @@ def save_url(url, filename, overwrite=False): if not os.path.exists(dirname): os.makedirs(dirname) data = read_url(url) - f = open(filename, 'w') - f.write(data) - f.close() + with open(filename, 'wb') as f: + f.write(data) def cache_path(): return os.environ.get('oxCACHE', os.path.expanduser('~/.ox/cache')) @@ -292,7 +291,7 @@ class FileCache(Cache): if value == 'headers': r = info['headers'] else: - with open(f) as data: + with open(f, 'rb') as data: r = data.read() if info['compressed']: r = zlib.decompress(r) @@ -323,9 +322,11 @@ class FileCache(Cache): if not info['only_headers']: if info['compressed']: data = zlib.compress(data) - with open(f, 'w') as _f: + elif not isinstance(data, str): + data = data.encode('utf-8') + with open(f, 'wb') as _f: _f.write(data) - with open(i, 'w') as _i: + with open(i, 'wb') as _i: json.dump(info, _i) if cache_path().startswith('fs:'):