Skip to content

Instantly share code, notes, and snippets.

@Chaz6
Created December 28, 2014 15:57
Show Gist options
  • Save Chaz6/f0cccc61038362acd955 to your computer and use it in GitHub Desktop.
Save Chaz6/f0cccc61038362acd955 to your computer and use it in GitHub Desktop.
Extract files recursively using python
#!/usr/bin/env python
import errno
import os
import sys
import subprocess
#-----------------------------------------------------------------------------
def main():
fileNames = sys.argv[1:]
for fileName in fileNames:
walkFiles(fileName)
#-----------------------------------------------------------------------------
def unzip(fileName):
oDir = getExpandedDirName(fileName)
mkdir_p(oDir)
print "Processing: %s into: %s" % (fileName, oDir)
process = subprocess.Popen(["unzip", "-n", "-d", oDir, fileName])
status = os.waitpid(process.pid, 0)[1]
if status == 0:
print "Deleting %s" % fileName
os.unlink(fileName)
walkFiles(oDir)
#-----------------------------------------------------------------------------
def unrar(fileName):
oDir = getExpandedDirName(fileName)
print "Processing: %s into %s" % (fileName, oDir)
mkdir_p(oDir)
process = subprocess.Popen(["unrar", "x", "-o-", fileName, oDir])
status = os.waitpid(process.pid, 0)[1]
if status == 0:
print "Deleting %s" % fileName
os.unlink(fileName)
walkFiles(oDir)
#-----------------------------------------------------------------------------
def untar(fileName):
oDir = getExpandedDirName(fileName)
print "Processing: %s into: %s" % (fileName, oDir)
mkdir_p(oDir)
process = subprocess.Popen(["tar", "axvfk", fileName, "-C", oDir])
status = os.waitpid(process.pid, 0)[1]
if status == 0:
print "Deleting %s" % fileName
os.unlink (fileName)
walkFiles(oDir)
#-----------------------------------------------------------------------------
def walkFiles(dirName):
dirs = os.walk(dirName)
for (dirPath, dirNames, fileNames) in dirs:
for dirName in dirNames:
walkFiles(os.path.join(dirPath, dirName))
for fileName in fileNames:
if isZipArchive(fileName):
unzip(os.path.join(dirPath, fileName))
if isRarArchive(fileName):
unrar(os.path.join(dirPath, fileName))
if isTarArchive(fileName):
untar(os.path.join(dirPath, fileName))
#-----------------------------------------------------------------------------
def getExpandedDirName(fileName):
fileDir = os.path.dirname(fileName)
baseName = "%s.contents" % os.path.basename(fileName)
return os.path.join(fileDir, baseName)
#-----------------------------------------------------------------------------
def isZipArchive(fileName):
ext = fileName[-4:]
if ext.lower() in [".zip"]: return True
return False
#-----------------------------------------------------------------------------
def isRarArchive(fileName):
ext = fileName[-4:]
if ext.lower() in [".rar"]: return True
return False
#-----------------------------------------------------------------------------
def isTarArchive(fileName):
ext = fileName[-4:]
if ext.lower() in [".tar", ".tgz"]: return True
return False
#-----------------------------------------------------------------------------
def mkdir_p(path):
try:
os.makedirs(path)
except OSError as exc:
if exc.errno == errno.EEXIST and os.path.isdir(path):
pass
else:
raise
#-----------------------------------------------------------------------------
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment