Last active
August 29, 2015 14:01
-
-
Save lamchau/ed59ef669dc4914fbbfb to your computer and use it in GitHub Desktop.
Random extraction of zipfile contents
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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