When trying to work with Linux archive downloaded onto Windows, sometimes it is helpful to untar all found archives, give extension-less files the "txt" extension, and convert text files to Windows line endings (\r\n), and that is just what this program is designed to accomplish. This is committed for archival to be run under Python 2.5 or later versions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | import os
import sys
import tarfile
ERROR = False
TABLE = ''.join(map(chr, range(256)))
DELETECHARS = ''.join(c for c in TABLE if len(repr(c)) != 6)
def main():
try:
arguments = sys.argv[1:]
assert arguments
for path in arguments:
assert os.path.isdir(path)
for function in (untar, bias, convert):
for path in arguments:
engine(path, function)
except:
sys.stdout.write(
'Usage: %s <directory>' % os.path.basename(sys.argv[0]))
def engine(path, function):
global ERROR
for root, dirs, files in os.walk(path):
for name in files:
path = os.path.join(root, name)
try:
function(path)
except:
sys.stderr.write('%sError: %s' % (ERROR and '\n' or '', path))
ERROR = True
def untar(path):
try: tarfile.open(path).extractall(os.path.dirname(path))
except: pass
def bias(path):
root, ext = os.path.splitext(path)
if not ext[1:]:
os.rename(path, root + '.txt')
def convert(path):
if not file(path, 'rb').read(2 ** 20).translate(TABLE, DELETECHARS):
data = file(path, 'r').read()
file(path, 'w').write(data)
if __name__ == '__main__':
main()
|
This is a small, old utility that may be of use to those trying to work with Linux code on Windows. It is a more advanced version of recipe 464413.