Saturday, 15 August 2015

Python cannot open absolute path in zipfile.ZipFile -



Python cannot open absolute path in zipfile.ZipFile -

i'm trying parse through grouping of zips in directory other cwd (and read csv file within of - i'm not concerned here), next code:

for name in glob.glob('/users/brendanmurphy/documents/chicago-data/16980*.zip'): base of operations = os.path.basename(name) filename = os.path.splitext(base)[0] datadirectory = '/users/brendanmurphy/documents/chicago-data/' fullpath = ''.join([datadirectory, base]) csv_file = '.'.join([filename, 'csv']) ozid, start, end = filename.split("-") zfile = zipfile.zipfile(fullpath)

but trying pass total path zipfile.zipfile gives next total traceback:

file "chicago_csv_reader.py", line 33, in <module> zfile = zipfile.zipfile(fullpath) file "/system/library/frameworks/python.framework/versions/2.7/lib/python2.7/zipfile.py", line 766, in __init__ self._realgetcontents() file "/system/library/frameworks/python.framework/versions/2.7/lib/python2.7/zipfile.py", line 807, in _realgetcontents raise badzipfile, "file not zip file" zipfile.badzipfile: file not zip file

what right way pass path of zip file not in cwd zipfile handler?

you aren't joining path , base of operations name properly.

do not do

filename = os.path.splitext(base)[0]

you stripping .zip extension file, means pointing somewhere else.

try generating fullpath so:

# utilize "base" string, not "filename" string! fullpath = os.path.join(datadirectory, base)

then sanity check before seek unzip file:

if not os.path.exists(fullpath): raise exception("path '{0}' not exist!".format(fullpath)) zfile = zipfile.zipfile(fullpath)

python path zip zipfile

No comments:

Post a Comment