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):
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:'):