本文整理匯總了Python中zipfile.ZipFile方法的典型用法代碼示例。如果您正苦於以下問題:Python zipfile.ZipFile方法的具體用法?Python zipfile.ZipFile怎麽用?Python zipfile.ZipFile使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類zipfile
的用法示例。
在下文中一共展示了zipfile.ZipFile方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: downloadDemo
# 需要導入模塊: import zipfile [as 別名]
# 或者: from zipfile import ZipFile [as 別名]
def downloadDemo(which):
try:
downloadDir = tempfile.mkdtemp()
archivePath = "{}/svviz-data.zip".format(downloadDir)
# logging.info("Downloading...")
downloadWithProgress("http://svviz.github.io/svviz/assets/examples/{}.zip".format(which), archivePath)
logging.info("Decompressing...")
archive = zipfile.ZipFile(archivePath)
archive.extractall("{}".format(downloadDir))
if not os.path.exists("svviz-examples"):
os.makedirs("svviz-examples/")
shutil.move("{temp}/{which}".format(temp=downloadDir, which=which), "svviz-examples/")
except Exception as e:
print("error downloading and decompressing example data: {}".format(e))
return False
if not os.path.exists("svviz-examples"):
print("error finding example data after download and decompression")
return False
return True
示例2: pack
# 需要導入模塊: import zipfile [as 別名]
# 或者: from zipfile import ZipFile [as 別名]
def pack(self) -> None:
"""
Pack all files in zip
"""
def rm_file(file: str):
if os.path.exists(file) and os.path.isfile(file):
os.remove(file)
def rename_file(file: str):
target = file + ".old"
rm_file(target)
if os.path.exists(file) and os.path.isfile(file):
os.rename(file, target)
self._check_files([self._weights_file, self._state_file])
rename_file(self._checkpoint_file)
with ZipFile(self._checkpoint_file, 'w') as zipfile:
zipfile.write(self._weights_file, os.path.basename(self._weights_file))
zipfile.write(self._state_file, os.path.basename(self._state_file))
zipfile.write(self._trainer_file, os.path.basename(self._trainer_file))
self.clear_files()
示例3: __call__
# 需要導入模塊: import zipfile [as 別名]
# 或者: from zipfile import ZipFile [as 別名]
def __call__(self, args, env):
samples_dir = env.get('samples-dir')
print("Downloading unique objects to {}.".format(samples_dir))
src_dir = self.download_files([_URL], env=env, dir=env.get('cache-dir'))
path = os.path.join(src_dir, "ObjectsAll.zip")
zipf = zipfile.ZipFile(path, 'r')
zipf.extractall(src_dir)
zipf.close()
for file in os.listdir(os.path.join(src_dir, "OBJECTSALL")):
shutil.copy(os.path.join(src_dir, "OBJECTSALL", file), samples_dir)
shutil.rmtree(src_dir)
print("Finished downloading unique objects.")
示例4: __call__
# 需要導入模塊: import zipfile [as 別名]
# 或者: from zipfile import ZipFile [as 別名]
def __call__(self, args, env):
samples_dir = env.get('samples-dir')
for label in ("cat", "dog"):
dest = os.path.join(samples_dir, label)
if os.path.exists(dest):
raise VergeMLError("Directory {} already exists in samples dir: {}".format(label, dest))
print("Downloading cats and dogs to {}.".format(samples_dir))
src_dir = self.download_files([(_URL, "catsdogs.zip")], env)
path = os.path.join(src_dir, "catsdogs.zip")
print("Extracting data.")
zipf = zipfile.ZipFile(path, 'r')
zipf.extractall(src_dir)
zipf.close()
for file, dest in (("PetImages/Dog", "dog"), ("PetImages/Cat", "cat")):
shutil.copytree(os.path.join(src_dir, file), os.path.join(samples_dir, dest))
shutil.rmtree(src_dir)
# WTF?
os.unlink(os.path.join(samples_dir, "cat", "666.jpg"))
os.unlink(os.path.join(samples_dir, "dog", "11702.jpg"))
print("Finished downloading cats and dogs.")
示例5: download_figshare
# 需要導入模塊: import zipfile [as 別名]
# 或者: from zipfile import ZipFile [as 別名]
def download_figshare(file_name, file_ext, dir_path='./', change_name = None):
prepare_data_dir(dir_path)
url = 'https://ndownloader.figshare.com/files/' + file_name
wget.download(url, out=dir_path)
file_path = os.path.join(dir_path, file_name)
if file_ext == '.zip':
zip_ref = zipfile.ZipFile(file_path,'r')
if change_name is not None:
dir_path = os.path.join(dir_path, change_name)
zip_ref.extractall(dir_path)
zip_ref.close()
os.remove(file_path)
elif file_ext == '.tar.bz2':
tar_ref = tarfile.open(file_path,'r:bz2')
if change_name is not None:
dir_path = os.path.join(dir_path, change_name)
tar_ref.extractall(dir_path)
tar_ref.close()
os.remove(file_path)
elif change_name is not None:
os.rename(file_path, os.path.join(dir_path, change_name))
# Download QM9 dataset
示例6: zip_stream
# 需要導入模塊: import zipfile [as 別名]
# 或者: from zipfile import ZipFile [as 別名]
def zip_stream(src_dir, output_stream):
"""
@param src_dir:
@type src_dir: str
@param output_stream:
@type output_stream: zipfile.ZipFile
@return:
"""
root_path = os.path.dirname(src_dir)
assert os.path.isdir(src_dir), "Invalid, not a directory: '{}'".format(src_dir)
for root, directories, files in os.walk(src_dir):
for file_name in files:
file_path = os.path.join(root, file_name)
relative_path = os.path.relpath(file_path, root_path)
output_stream.write(file_path, arcname=relative_path)
示例7: get_mnist
# 需要導入模塊: import zipfile [as 別名]
# 或者: from zipfile import ZipFile [as 別名]
def get_mnist(data_dir):
if not os.path.isdir(data_dir):
os.system("mkdir " + data_dir)
os.chdir(data_dir)
if (not os.path.exists('train-images-idx3-ubyte')) or \
(not os.path.exists('train-labels-idx1-ubyte')) or \
(not os.path.exists('t10k-images-idx3-ubyte')) or \
(not os.path.exists('t10k-labels-idx1-ubyte')):
import urllib, zipfile
zippath = os.path.join(os.getcwd(), "mnist.zip")
urllib.urlretrieve("http://data.mxnet.io/mxnet/data/mnist.zip", zippath)
zf = zipfile.ZipFile(zippath, "r")
zf.extractall()
zf.close()
os.remove(zippath)
os.chdir("..")
示例8: get_cifar10
# 需要導入模塊: import zipfile [as 別名]
# 或者: from zipfile import ZipFile [as 別名]
def get_cifar10(data_dir):
if not os.path.isdir(data_dir):
os.system("mkdir " + data_dir)
cwd = os.path.abspath(os.getcwd())
os.chdir(data_dir)
if (not os.path.exists('train.rec')) or \
(not os.path.exists('test.rec')) :
import urllib, zipfile, glob
dirname = os.getcwd()
zippath = os.path.join(dirname, "cifar10.zip")
urllib.urlretrieve("http://data.mxnet.io/mxnet/data/cifar10.zip", zippath)
zf = zipfile.ZipFile(zippath, "r")
zf.extractall()
zf.close()
os.remove(zippath)
for f in glob.glob(os.path.join(dirname, "cifar", "*")):
name = f.split(os.path.sep)[-1]
os.rename(f, os.path.join(dirname, name))
os.rmdir(os.path.join(dirname, "cifar"))
os.chdir(cwd)
# data
示例9: _get_data
# 需要導入模塊: import zipfile [as 別名]
# 或者: from zipfile import ZipFile [as 別名]
def _get_data(self):
archive_file_name, archive_hash = self._archive_file
data_file_name, data_hash = self._data_file[self._segment]
path = os.path.join(self._root, data_file_name)
if not os.path.exists(path) or not check_sha1(path, data_hash):
namespace = 'gluon/dataset/'+self._namespace
downloaded_file_path = download(_get_repo_file_url(namespace, archive_file_name),
path=self._root,
sha1_hash=archive_hash)
with zipfile.ZipFile(downloaded_file_path, 'r') as zf:
for member in zf.namelist():
filename = os.path.basename(member)
if filename:
dest = os.path.join(self._root, filename)
with zf.open(member) as source, \
open(dest, "wb") as target:
shutil.copyfileobj(source, target)
data, label = self._read_batch(path)
self._data = nd.array(data, dtype=data.dtype).reshape((-1, self._seq_len))
self._label = nd.array(label, dtype=label.dtype).reshape((-1, self._seq_len))
示例10: extract
# 需要導入模塊: import zipfile [as 別名]
# 或者: from zipfile import ZipFile [as 別名]
def extract(self):
if(hasattr(cherrypy.request, 'json')):
if('dir' in cherrypy.request.json and cherrypy.request.json['dir']!='' and 'name' in cherrypy.request.json and cherrypy.request.json['name']!=''):
dir = cherrypy.request.json['dir']
name = cherrypy.request.json['name']
fdir = path.join(self.datapath, dir)
fp = path.join(fdir, name)
if(path.exists(fp)):
with zipfile.ZipFile(fp, "r") as z:
z.extractall(fdir)
return {'status': 'success', 'message': "File %s/%s extracted!"%(dir, name)}
else:
return {'status': 'error', 'message': "File doesn't exist!"}
else:
return {'status': 'error', 'message': "Invalid filename!"}
else:
return {'status': 'error', 'message': "No filename given!"}
示例11: execute
# 需要導入模塊: import zipfile [as 別名]
# 或者: from zipfile import ZipFile [as 別名]
def execute(self, config, args):
"""
Execution Hook
When the plugin command is provided the plugin from the zip file specified in the plugin
argument is installed in the Skelebot Plugins folder inside Skelebot Home
"""
# Create the ~/.skelebot directory if not already present
skelebotHome = os.path.expanduser(SKELEBOT_HOME)
if (os.path.exists(skelebotHome) is False):
os.makedirs(skelebotHome, exist_ok=True)
# Create the ~/.skelebot/plugins directory if not already present
pluginsHome = os.path.expanduser(PLUGINS_HOME)
if (os.path.exists(pluginsHome) is False):
os.makedirs(pluginsHome, exist_ok=True)
# Unzip the plugin into the plugins folder
zip_ref = zipfile.ZipFile(args.plugin, 'r')
zip_ref.extractall(pluginsHome)
zip_ref.close()
示例12: save
# 需要導入模塊: import zipfile [as 別名]
# 或者: from zipfile import ZipFile [as 別名]
def save(self, path=None):
"""Save model to a pickle located at `path`"""
if path is None:
path = os.path.join(logger.get_dir(), "model.pkl")
with tempfile.TemporaryDirectory() as td:
save_state(os.path.join(td, "model"))
arc_name = os.path.join(td, "packed.zip")
with zipfile.ZipFile(arc_name, 'w') as zipf:
for root, dirs, files in os.walk(td):
for fname in files:
file_path = os.path.join(root, fname)
if file_path != arc_name:
zipf.write(file_path, os.path.relpath(file_path, td))
with open(arc_name, "rb") as f:
model_data = f.read()
with open(path, "wb") as f:
cloudpickle.dump((model_data, self._act_params), f)
示例13: pickle_load
# 需要導入模塊: import zipfile [as 別名]
# 或者: from zipfile import ZipFile [as 別名]
def pickle_load(path, compression=False):
"""Unpickle a possible compressed pickle.
Parameters
----------
path: str
path to the output file
compression: bool
if true assumes that pickle was compressed when created and attempts decompression.
Returns
-------
obj: object
the unpickled object
"""
if compression:
with zipfile.ZipFile(path, "r", compression=zipfile.ZIP_DEFLATED) as myzip:
with myzip.open("data") as f:
return pickle.load(f)
else:
with open(path, "rb") as f:
return pickle.load(f)
示例14: install_hanlp_data
# 需要導入模塊: import zipfile [as 別名]
# 或者: from zipfile import ZipFile [as 別名]
def install_hanlp_data(the_jar_version=None):
if not the_jar_version:
the_jar_version = HANLP_JAR_VERSION if HANLP_JAR_VERSION else hanlp_latest_version()[0]
for jar_version, jar_url, data_version, data_url in hanlp_releases():
if jar_version == the_jar_version:
if data_version == hanlp_installed_data_version():
return False
data_zip = 'data-for-{}.zip'.format(data_version)
data_zip = os.path.join(STATIC_ROOT, data_zip)
download(data_url, os.path.join(STATIC_ROOT, data_zip))
print('解壓 data.zip...')
with zipfile.ZipFile(data_zip, "r") as zip_ref:
zip_ref.extractall(STATIC_ROOT)
os.remove(data_zip)
write_config(root=STATIC_ROOT)
with open_(PATH_DATA_VERSION, 'w', encoding='utf-8') as f:
f.write(data_version)
global HANLP_DATA_VERSION
HANLP_DATA_VERSION = data_version
return True
示例15: save_act
# 需要導入模塊: import zipfile [as 別名]
# 或者: from zipfile import ZipFile [as 別名]
def save_act(self, path=None):
"""Save model to a pickle located at `path`"""
if path is None:
path = os.path.join(logger.get_dir(), "model.pkl")
with tempfile.TemporaryDirectory() as td:
save_state(os.path.join(td, "model"))
arc_name = os.path.join(td, "packed.zip")
with zipfile.ZipFile(arc_name, 'w') as zipf:
for root, dirs, files in os.walk(td):
for fname in files:
file_path = os.path.join(root, fname)
if file_path != arc_name:
zipf.write(file_path, os.path.relpath(file_path, td))
with open(arc_name, "rb") as f:
model_data = f.read()
with open(path, "wb") as f:
cloudpickle.dump((model_data, self._act_params), f)