more file open py2/3 cleanups

This commit is contained in:
j 2014-10-02 10:34:04 +02:00
parent 37dfed3143
commit 970f37c38c

View file

@ -119,9 +119,8 @@ def save_url(url, filename, overwrite=False):
if not os.path.exists(dirname): if not os.path.exists(dirname):
os.makedirs(dirname) os.makedirs(dirname)
data = read_url(url) data = read_url(url)
f = open(filename, 'w') with open(filename, 'wb') as f:
f.write(data) f.write(data)
f.close()
def cache_path(): def cache_path():
return os.environ.get('oxCACHE', os.path.expanduser('~/.ox/cache')) return os.environ.get('oxCACHE', os.path.expanduser('~/.ox/cache'))
@ -292,7 +291,7 @@ class FileCache(Cache):
if value == 'headers': if value == 'headers':
r = info['headers'] r = info['headers']
else: else:
with open(f) as data: with open(f, 'rb') as data:
r = data.read() r = data.read()
if info['compressed']: if info['compressed']:
r = zlib.decompress(r) r = zlib.decompress(r)
@ -323,9 +322,11 @@ class FileCache(Cache):
if not info['only_headers']: if not info['only_headers']:
if info['compressed']: if info['compressed']:
data = zlib.compress(data) 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) _f.write(data)
with open(i, 'w') as _i: with open(i, 'wb') as _i:
json.dump(info, _i) json.dump(info, _i)
if cache_path().startswith('fs:'): if cache_path().startswith('fs:'):