Created
February 25, 2014 21:59
-
-
Save diopib/9218771 to your computer and use it in GitHub Desktop.
archive smart extractor
This file contains 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
__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