我设法让我的第一个python脚本工作,从URL下载.ZIP文件列表,然后继续提取ZIP文件并将它们写入磁盘.
我现在无法实现下一步.
我的主要目标是下载并解压缩zip文件,并通过TCP流传递内容(CSV数据).如果我能逃脱它,我宁愿不将任何zip或解压缩的文件写入磁盘.
这是我当前的脚本,但不幸的是必须将文件写入磁盘.
import urllib, urllister
import zipfile
import urllib2
import os
import time
import pickle
# check for extraction directories existence
if not os.path.isdir('downloaded'):
os.makedirs('downloaded')
if not os.path.isdir('extracted'):
os.makedirs('extracted')
# open logfile for downloaded data and save to local variable
if os.path.isfile('downloaded.pickle'):
downloadedLog = pickle.load(open('downloaded.pickle'))
else:
downloadedLog = {'key':'value'}
# remove entries older than 5 days (to maintain speed)
# path of zip files
zipFileURL = "http://www.thewebserver.com/that/contains/a/directory/of/zip/files"
# retrieve list of URLs from the webservers
usock = urllib.urlopen(zipFileURL) …
Run Code Online (Sandbox Code Playgroud) 如何将zip解压缩到内存中?
我尝试(返回None
上.getvalue()
):
from zipfile import ZipFile
from StringIO import StringIO
def extract_zip(input_zip):
return StringIO(ZipFile(input_zip).extractall())
Run Code Online (Sandbox Code Playgroud)