我正在尝试解析cwd以外的目录中的一组zip文件(然后在其中读取一个csv文件 - 我不关心这里),使用以下代码:
for name in glob.glob('/Users/brendanmurphy/Documents/chicago-data/16980*.zip'):
base = 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)
但是尝试将完整路径传递给zipfile.ZipFile会给出以下完整的回溯:
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 is not a zip file"
zipfile.BadZipfile: File is not a zip file
将不在cwd中的zip文件的路径传递给ZipFile处理程序的正确方法是什么?
答案 0 :(得分:0)
您没有正确加入路径和基本名称。
不要做
filename = os.path.splitext(base)[0]
您正在从文件中删除.zip
扩展名,这意味着您指向其他位置。
尝试生成fullpath
,如下所示:
# Use your "base" string, not your "filename" string!
fullpath = os.path.join(datadirectory, base)
然后在尝试解压缩文件之前进行完整性检查:
if not os.path.exists(fullpath):
raise Exception("Path '{0}' does not exist!".format(fullpath))
zfile = zipfile.ZipFile(fullpath)
答案 1 :(得分:0)
你有几个问题。首先,&#39; name&#39;已经是zipfile名称,你不需要做任何事情。其次,&#39; .join([datadirectory,base])只是附加两个名称并省略路径分隔符。这应该有效:
datadirectory = '/Users/brendanmurphy/Documents/chicago-data/'
for name in glob.glob('/Users/brendanmurphy/Documents/chicago-data/16980*.zip'):
filename = os.path.splitext(base)[0]
csv_file = os.path.join(datadirectory, filename + '.csv')
ozid, start, end = filename.split("-")
zfile = zipfile.ZipFile(name)