Skip to content

Instantly share code, notes, and snippets.

@diopib
Created February 25, 2014 21:59
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 diopib/9218771 to your computer and use it in GitHub Desktop.
Save diopib/9218771 to your computer and use it in GitHub Desktop.
archive smart extractor
__author__ = "Ibrahim Diop <http://ibrahim.zinaria.com>"
import sh # http://amoffat.github.io/sh/
import os
import ntpath
def smart_extract(dirname):
"""
extract all zip file contained in specified directory
- if a zip file contains multiple files and/or directories, all files and directories are
extracted to a directory that has the same name as the zip file without the extension
- if the zip file contain a single directory, that directory is extracted
@param dirname: directory where zip files are located
"""
file_list = file_list = sh.ls(sh.glob('{}*.zip'.format(dirname))).split('\n')
for f in file_list:
print('processing zip file: {}'.format(f))
tempdir = '{}temp/'.format(dirname)
sh.unzip('-d', tempdir, f)
if len(sh.ls(tempdir).split('\n')) == 2:
sh.mv(sh.glob('{}*'.format(tempdir)), '/home/cms/Music/')
sh.rm('-r', tempdir)
else:
sh.mv(tempdir, '{0}{1}'.format(dirname, os.path.splitext(ntpath.basename(f))[0]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment