Can't import python library 'zipfile'
Solution 1
Two ways to fix it:
1) use from
, and in that case drop the zipfile
namespace:
from zipfile import *
#set filename
fpath = '{}_{}_{}.zip'.format(strDate, day, week)
#use zipfile to get info about ftp file
zip = ZipFile(fpath, mode='r')
2) use direct import
, and in that case use full path like you did:
import zipfile
#set filename
fpath = '{}_{}_{}.zip'.format(strDate, day, week)
#use zipfile to get info about ftp file
zip = zipfile.ZipFile(fpath, mode='r')
and there's a sneaky typo in your code: Zipfile
should be ZipFile
(capital F
, so I feel slightly bad for answering...
So the lesson learnt is:
- avoid
from x import y
because editors have a harder time to complete words - with a proper
import zipfile
and an editor which proposes completion, you would never have had this problem in the first place.
Solution 2
Easiest way to zip a file using Python:
import zipfile
zf = zipfile.ZipFile("targetZipFileName.zip",'w', compression=zipfile.ZIP_DEFLATED)
zf.write("FileTobeZipped.txt")
zf.close()
REdim.Learning
Chemist turned data analyst, finding my home in programming. Big fan of interactive data visualisation, but focused on creating amazing spreadsheets.
Updated on June 05, 2022Comments
-
REdim.Learning almost 2 years
Feel like a dunce. I'm trying to interact with a zip file and can't seem to use the zipfile library. Fairly new to python
from zipfile import * #set filename fpath = '{}_{}_{}.zip'.format(strDate, day, week) #use zipfile to get info about ftp file zip = zipfile.Zipfile(fpath, mode='r') # doesn't matter if I use #zip = zipfile.Zipfile(fpath, mode='w') #or zip = zipfile.Zipfile(fpath, 'wb')
I'm getting this error
zip = zipfile.Zipfile(fpath, mode='r')
NameError: name 'zipfile' is not defined
if I just use import zipfile I get this error:
TypeError: 'module' object is not callable