We know, if we use the requests to open the url of the zip file and open it, obviously, it will take quite a long time.
For instance
def getZIP(zipFileName):
r = requests.get(zipFileName).content
s = StringIO.StringIO(r)
zf = zipfile.ZipFile(s, 'r') # Read in a list of zipped files
return zf
url = 'http://seanlahman.com/files/database/lahman-csv_2014-02-14.zip'
zf = getZIP(url)
tablenames = zf.namelist()
print tablenames
But
- Use the Browser to download the online file is fast(
, Browser is powerful)
- Use the open clause to open the host location path file and return the File Object
def getZIP(zipFileName):
zf = zipfile.ZipFile(zipFileName, 'r')
return zf
fileobj = '/home/xxx/temp/lahman-csv_2014-02014/lahman-csv_2014-02-14.zip'
zf = getZIP(fileobj)
tablenames = zf.namelist()
print tablenames