zipfile.is_zipfile() and string buffers
Gabriel Genellina
gagsl-py2 at yahoo.com.ar
Tue Dec 16 11:59:36 EST 2008
En Tue, 16 Dec 2008 12:28:00 -0200, Brendan <brendandetracey at yahoo.com>
escribió:
> I would like zipfile.is_zipfile(), to operate on a cStringIO.StringIO
> string buffer, but is seems only to accept file names as arguments.
> Should it not be able to handle string buffers too?
A version of zipfile.is_zipfile() accepting both file names and file
objects:
def _check_zipfile(fp):
try:
if _EndRecData(fp):
return True # file has correct magic number
except IOError:
pass
return False
def is_zipfile(filename):
"""Quickly see if file is a ZIP file by checking the magic number."""
result = False
try:
if hasattr(filename, "read"):
result = _check_zipfile(fp=filename)
else:
with open(filename, "rb") as fp:
result = _check_zipfile(fp)
except IOError:
pass
return result
--
Gabriel Genellina
More information about the Python-list
mailing list