Skip to content

Instantly share code, notes, and snippets.

@lamchau
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lamchau/ed59ef669dc4914fbbfb to your computer and use it in GitHub Desktop.
Save lamchau/ed59ef669dc4914fbbfb to your computer and use it in GitHub Desktop.
Random extraction of zipfile contents
#!/usr/bin/env python
import os
import random
import zipfile
def is_zipfile(f):
return os.path.isfile(f) and os.path.splitext(f)[1].lower() == ".zip"
def unzip(f, target_dir):
zfile = zipfile.ZipFile(f)
selected_filename = random.choice(zfile.namelist())
(directory, filename) = os.path.split(selected_filename)
if len(filename):
print(" %s" % filename)
zfile.extract(filename, target_dir)
zfile.close()
target_dir = "samples"
print("extracting to '%s'" % os.path.abspath(target_dir))
for f in os.listdir():
if is_zipfile(f):
unzip(f, target_dir)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment