more file open py2/3 cleanups
This commit is contained in:
parent
37dfed3143
commit
970f37c38c
1 changed files with 7 additions and 6 deletions
13
ox/cache.py
13
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:'):
|
||||
|
|
Loading…
Reference in a new issue