Last active
January 7, 2021 02:03
-
-
Save kzinmr/3587989376ded68bd2d4f9d488e67ff0 to your computer and use it in GitHub Desktop.
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
import os | |
import shutil | |
import sys | |
import zipfile | |
def copy_docs(zip_path, p): | |
try: | |
with zipfile.ZipFile(zip_path) as z: | |
targets = [fn for fn in z.namelist() if fn.endswith('doc') or fn.endswith('docx')] | |
for fn in targets: | |
with z.open(fn) as zf: | |
try: | |
newpath = os.path.join(p, fn.split('\\')[-1]) | |
if not os.path.exists(newpath): | |
with open(newpath, 'wb') as f: | |
shutil.copyfileobj(zf, f) | |
except IOError: | |
newpath = os.path.join(p, fn.split('\\')[-1][-30:]) | |
if not os.path.exists(newpath): | |
with open(newpath, 'wb') as f: | |
shutil.copyfileobj(zf, f) | |
except zipfile.BadZipfile: | |
print(zip_path) | |
if __name__=='__main__': | |
args = sys.argv | |
assert len(args) == 3 | |
zip_dir = args[1] | |
p = args[2] | |
for cur, dirs, files in os.walk(zip_dir): | |
subdir = os.path.join(p, os.path.basename(cur)) | |
for fn in files: | |
zip_path = os.path.join(cur, fn) | |
target_dir = os.path.join(subdir, os.path.basename(zip_path).replace('.zip', '')) | |
if not os.path.exists(target_dir): | |
os.makedirs(target_dir) | |
copy_docs(zip_path, target_dir) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment