问题遇到的现象和发生背景
就是在接API的时候,需要将数据以zip格式下载保存到本地,然后再解压,解析上传到数据库,但是在用zipfile,按照网上的方法,传入路径、传入文件名,最终都会报错:zipfile.BadZipFile: File is not a zip file
问题相关代码,请勿粘贴截图
def unzip_file(zip_file,target_dir):
with zipfile.ZipFile(zip_file, "r") as zfile:
for file in zfile.namelist():
zfile.extract(file, target_dir)
运行结果及报错内容
Traceback (most recent call last):
File "/Users/micra/PycharmProjects/pythonProject2/搜狗API.py", line 92, in
run()
File "/Users/micra/PycharmProjects/pythonProject2/搜狗API.py", line 87, in run
unzip_file("demo1.zip", "demo")
File "/Users/micra/PycharmProjects/pythonProject2/搜狗API.py", line 65, in unzip_file
z = zipfile.ZipFile(zip_file, "r")
File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/zipfile.py", line 1269, in init
self._RealGetContents()
File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/zipfile.py", line 1336, in _RealGetContents
raise BadZipFile("File is not a zip file")
zipfile.BadZipFile: File is not a zip file
进程已结束,退出代码为 1