本文整理匯總了Python中zipfile.namelist方法的典型用法代碼示例。如果您正苦於以下問題:Python zipfile.namelist方法的具體用法?Python zipfile.namelist怎麽用?Python zipfile.namelist使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類zipfile
的用法示例。
在下文中一共展示了zipfile.namelist方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __init__
# 需要導入模塊: import zipfile [as 別名]
# 或者: from zipfile import namelist [as 別名]
def __init__(self, zipfile, entry=''):
"""
Create a new path pointer pointing at the specified entry
in the given zipfile.
:raise IOError: If the given zipfile does not exist, or if it
does not contain the specified entry.
"""
if isinstance(zipfile, string_types):
zipfile = OpenOnDemandZipFile(os.path.abspath(zipfile))
# Normalize the entry string, it should be relative:
entry = normalize_resource_name(entry, True, '/').lstrip('/')
# Check that the entry exists:
if entry:
try:
zipfile.getinfo(entry)
except Exception:
# Sometimes directories aren't explicitly listed in
# the zip file. So if `entry` is a directory name,
# then check if the zipfile contains any files that
# are under the given directory.
if (entry.endswith('/') and
[n for n in zipfile.namelist() if n.startswith(entry)]):
pass # zipfile contains a file in that directory.
else:
# Otherwise, complain.
raise IOError('Zipfile %r does not contain %r' %
(zipfile.filename, entry))
self._zipfile = zipfile
self._entry = entry
示例2: get_original_crash_test_case_of_zipfile
# 需要導入模塊: import zipfile [as 別名]
# 或者: from zipfile import namelist [as 別名]
def get_original_crash_test_case_of_zipfile(crash_test_case, original_test_case):
import zipfile
zipfile = zipfile.ZipFile(io.BytesIO(original_test_case))
max_similarity = 0
for name in zipfile.namelist():
possible_original_test_case = zipfile.read(name)
similarity = SequenceMatcher(None, base64.b64encode(possible_original_test_case),
base64.b64encode(crash_test_case)).ratio()
if similarity > max_similarity:
max_similarity = similarity
original_test_case = possible_original_test_case
return original_test_case
示例3: test_build
# 需要導入模塊: import zipfile [as 別名]
# 或者: from zipfile import namelist [as 別名]
def test_build(self):
builder = Builder('test.zip')
builder._runtime = RUNTIME_NODE_JS
ok_(hasattr(builder.build(), 'read'))
with PyZipFile(builder._zippath, 'r', compression=ZIP_DEFLATED) as zipfile:
ok_('lambda_function.pyc' in zipfile.namelist())
ok_('.lamvery_secret.json' in zipfile.namelist())
示例4: test_build_with_single_file
# 需要導入模塊: import zipfile [as 別名]
# 或者: from zipfile import namelist [as 別名]
def test_build_with_single_file(self):
builder = Builder('test.zip', function_filename='lambda_function.py', single_file=True)
builder.build()
with PyZipFile(builder._zippath, 'r', compression=ZIP_DEFLATED) as zipfile:
ok_('lambda_function.py' in zipfile.namelist())
ok_(not ('.lamvery_secret.json' in zipfile.namelist()))
示例5: __init__
# 需要導入模塊: import zipfile [as 別名]
# 或者: from zipfile import namelist [as 別名]
def __init__(self, zipfile, entry=''):
"""
Create a new path pointer pointing at the specified entry
in the given zipfile.
:raise IOError: If the given zipfile does not exist, or if it
does not contain the specified entry.
"""
if isinstance(zipfile, basestring):
zipfile = OpenOnDemandZipFile(os.path.abspath(zipfile))
# Normalize the entry string:
entry = re.sub('(^|/)/+', r'\1', entry)
# Check that the entry exists:
if entry:
try: zipfile.getinfo(entry)
except:
# Sometimes directories aren't explicitly listed in
# the zip file. So if `entry` is a directory name,
# then check if the zipfile contains any files that
# are under the given directory.
if (entry.endswith('/') and
[n for n in zipfile.namelist() if n.startswith(entry)]):
pass # zipfile contains a file in that directory.
else:
# Otherwise, complain.
raise IOError('Zipfile %r does not contain %r' %
(zipfile.filename, entry))
self._zipfile = zipfile
self._entry = entry
示例6: __init__
# 需要導入模塊: import zipfile [as 別名]
# 或者: from zipfile import namelist [as 別名]
def __init__(self, zipfile, entry=''):
"""
Create a new path pointer pointing at the specified entry
in the given zipfile.
:raise IOError: If the given zipfile does not exist, or if it
does not contain the specified entry.
"""
if isinstance(zipfile, string_types):
zipfile = OpenOnDemandZipFile(os.path.abspath(zipfile))
# Check that the entry exists:
if entry:
# Normalize the entry string, it should be relative:
entry = normalize_resource_name(entry, True, '/').lstrip('/')
try:
zipfile.getinfo(entry)
except Exception:
# Sometimes directories aren't explicitly listed in
# the zip file. So if `entry` is a directory name,
# then check if the zipfile contains any files that
# are under the given directory.
if entry.endswith('/') and [
n for n in zipfile.namelist() if n.startswith(entry)
]:
pass # zipfile contains a file in that directory.
else:
# Otherwise, complain.
raise IOError(
'Zipfile %r does not contain %r' % (zipfile.filename, entry)
)
self._zipfile = zipfile
self._entry = entry
示例7: _analyze_zipfile_for_import
# 需要導入模塊: import zipfile [as 別名]
# 或者: from zipfile import namelist [as 別名]
def _analyze_zipfile_for_import(zipfile, project, schema):
names = zipfile.namelist()
def read_sp_manifest_file(path):
# Must use forward slashes, not os.path.sep.
fn_manifest = path + '/' + project.Job.FN_MANIFEST
if fn_manifest in names:
return json.loads(zipfile.read(fn_manifest).decode())
if schema is None:
schema_function = read_sp_manifest_file
elif callable(schema):
schema_function = _with_consistency_check(schema, read_sp_manifest_file)
elif isinstance(schema, str):
schema_function = _with_consistency_check(
_make_path_based_schema_function(schema), read_sp_manifest_file)
else:
raise TypeError("The schema variable must be None, callable, or a string.")
mappings = dict()
skip_subdirs = set()
dirs = {os.path.dirname(name) for name in names}
for name in sorted(dirs):
cont = False
for skip in skip_subdirs:
if name.startswith(skip):
cont = True
break
if cont:
continue
sp = schema_function(name)
if sp is not None:
job = project.open_job(sp)
if os.path.exists(job.workspace()):
raise DestinationExistsError(job)
mappings[name] = job
skip_subdirs.add(name)
# Check uniqueness
if len(set(mappings.values())) != len(mappings):
raise RuntimeError("The jobs identified with the given schema function are not unique!")
for path, job in mappings.items():
_names = [name for name in names if name.startswith(path)]
yield path, _CopyFromZipFileExecutor(zipfile, path, job, _names)