EvilZone
Programming and Scripting => Scripting Languages => Topic started by: techb on May 29, 2013, 05:13:21 AM
-
My own util for tar files and such. Been using it for a while with downloads and such. One file at a time though, haven't expanded since I haven't had a reason too and all.
If you can make it better and expand then do so. I find this much easier than using untar because of the ridiculous flags for file types and all. Hashbangs may very, on Arch ATM.
-----------EDIT------------------------
The code posted before wasn't the right one. This is the code currently in use under /usr/bin.
What you seen before was just messing around and thoughts on error checking/etc...
Python 2.x
#!/usr/bin/python2
import tarfile
import os, sys
def unCompress(f):
os.mkdir(f.split('.')[0])
t = tarfile.open(f, 'r')
t.extractall(f.split('.')[0])
print sys.argv
if len(sys.argv) >= 2:
unCompress(sys.argv[1])
else:
print "Specify a file"
-
I think your code got mangled a bit; I'm not sure what check() or sys.ext() are supposed to do. But anyway, I've thought about writing a similar tool :) so I've made some improvements. Added support for zip files and processing multiple files at once.
#!/usr/bin/env python
# ^ leverage env for a universal hashbang
# USAGE: ./unarchive file1 file2 file3 ...
import tarfile
import zipfile
import os
def is_zipfile(fname):
return fname.split('.')[-1].lower() == "zip"
def extract(fname, path="."):
if is_zipfile(fname):
archive = zipfile.ZipFile(fname, 'r')
else:
archive = tarfile.open(fname, "r:*")
archive.extractall(path)
if __name__ == "__main__":
import sys
files = sys.argv[1:]
for fname in files:
path = fname.split(".")[0]
print("Extracting %s" % (fname)) # Treat print as a function, env might
extract(fname, path) # find python3 instead of python2.
-
I think your code got mangled a bit; I'm not sure what check() or sys.ext() are supposed to do. But anyway, I've thought about writing a similar tool :) so I've made some improvements. Added support for zip files and processing multiple files at once.
#!/usr/bin/env python
# ^ leverage env for a universal hashbang
# USAGE: ./unarchive file1 file2 file3 ...
import tarfile
import zipfile
import os
def is_zipfile(fname):
return fname.split('.')[-1].lower() == "zip"
def extract(fname, path="."):
if is_zipfile(fname):
archive = zipfile.ZipFile(fname, 'r')
else:
archive = tarfile.open(fname, "r:*")
archive.extractall(path)
if __name__ == "__main__":
import sys
files = sys.argv[1:]
for fname in files:
path = fname.split(".")[0]
print("Extracting %s" % (fname)) # Treat print as a function, env might
extract(fname, path) # find python3 instead of python2.
Updated the OP a bit I guess, it was quick, dirty, and meant for usefulness for me. I like your version though and might compound on it and what not later on or something. I can see needed support for other compression types and all.
+1 though.